468,242 Members | 1,605 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

cin.get loop help

4
When cin.get is in a loop, the second time through it does not store a value. I have it followed by a cin.ignore. Typical code would be

Expand|Select|Wrap|Line Numbers
  1. void info()
  2. {
  3.     char name[99],name2[99];
  4.     cout<<"Give me a palindrome.\n";
  5.     cin.get(name,99);
  6.     cin.ignore(10, '\n');
  7. }
  8.  
  9. main ()
  10. {
  11.     clrscr();
  12.     int choice;
  13.     do
  14.     {
  15.     info();
  16.     cin>>choice;
  17.     }
  18.     while (choice==1);
  19. }

Any suggestions??
Jan 23 '07 #1
4 2457
horace1
1,510 Expert 1GB
the problem is probably the newline you entered after typing in choice - it is left in the input stream and cin.get(name,99) immediatly exits. try cin.get() after reading choice
Expand|Select|Wrap|Line Numbers
  1.  cin>>  choice;
  2.  cin.get();    // remove newline
  3.  
Jan 23 '07 #2
chad05
4
I forgot a line of code. Sorry. It always works fine the first time, but the loops never work right.


void info()
{
char name[99],name2[99];
cout<<"Give me a palindrome.\n";
cin.get(name,99);
cin.ignore(10, '\n');
}

main ()
{
clrscr();
int choice;
do
{
info();
cout<<"Do you want to run this again? (1 for yes) \n"; //forgot this line
cin>>choice;
}
while (choice==1);
}
Jan 24 '07 #3
chad05
4
getting rid of the newline work as far as getting repeated loops, but now it won't store the input to the variable.
Jan 24 '07 #4
chad05
4
ok...nevermind...what you told me to do worked :)
Jan 24 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Tim::.. | last post: by
3 posts views Thread by Gustavo Randich | last post: by
6 posts views Thread by Shill | last post: by
32 posts views Thread by cj | last post: by
4 posts views Thread by joaotsetsemoita | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.