Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
}
Posted
Updated 24-Apr-16 10:15am
v3
Comments
Mircea Diaconescu 24-Apr-16 16:04pm    
You should definitely format your code, otherwise no one will read and answer your question.
Patrice T 24-Apr-16 16:13pm    
just done for the OP. :-)
Patrice T 24-Apr-16 16:20pm    
The changes I done in your question was only to make it more legible !
You don't like it ?
Mircea Diaconescu 24-Apr-16 16:24pm    
Well, why to keep the code formatted, it looks fancier now, that is back to its original ugly and unreadable form...
Patrice T 24-Apr-16 16:35pm    
Your choice ! you like fancy code and helpers like it formatted. :-)

1 solution

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[^]
 
Share this answer
 
Comments
Member 12270086 24-Apr-16 16:22pm    
debugger just corosses getline() taking len=0,and looping 100 times and from second iteration of outer for loop it's asking input from getline()...and taking input works correctly but why it's not working for first time...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900