getline()函数中的问题...... [英] Problems in getline()function...

查看:58
本文介绍了getline()函数中的问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在循环中第一次给出任何输入之前它会自动打印INVALID然后它正常工作在哪里故障无法理解帮助请....



我尝试过:



#include< iostream>

#include

使用命名空间std;



int main()

{

int n;

while(cin>> n)

{



for(int i = 0; i< n; i ++)>

{





string s;

getline( cin,s);



int len = s.size(),flag = 0;

for(int i = 1; i< ; 100; i ++)

{

int x = pow(i,2);



if( x == len)

{

flag = 1;

休息;



}



}

int len2 = pow(len,.5);



if(flag == 0)

cout<<INVALID\\\
;



else

{

int p = 0;



char c [len2] [len2 ];

for(int i = 0; i< len2; i ++)>

{

for(int j = 0; j< ; len2; j ++)>

{

c [i] [j] = s [p];

p ++;



}

}



for(int j = 0; j< 4; j ++ )

{

for(int i = 0; i< 4; i = i + 1)

{



cout<< c [i] [j];



}



}

cout<< endl;







}



}



}



返回0;

}

in the loop for the first time before giving any input it autometically prints INVALID then it works corrctly where is the fault cann't understand help please....

What I have tried:

#include <iostream>
#include
using namespace std;

int main()
{
int n;
while(cin>>n)
{

for(int i=0;i<n;i++)>
{


string s;
getline(cin,s);

int len=s.size(),flag=0;
for(int i=1;i<100;i++)
{
int x=pow(i,2);

if(x==len)
{
flag=1;
break;

}

}
int len2=pow(len,.5);

if(flag==0)
cout<<"INVALID\n";

else
{
int p=0;

char c [len2][len2];
for(int i=0;i<len2;i++)>
{
for(int j=0;j<len2;j++)>
{
c[i][j]=s[p];
p++;

}
}

for(int j=0;j<4;j++)
{
for(int i=0;i<4;i=i+1)
{

cout<<c[i][j];

}

}
cout<<endl;



}

}

}

return 0;
}

推荐答案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于getline()函数中的问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆