At school I am taking a c++ class, and in school we use codewarrior for the mac. At home tho I use Dev c++ (much better than codewarrior).
My problem occurs when I try to input to strings right after each other:
cout << "\nPlayer 1, please enter your name: ";
gets(player_1);
cout << "\nPlayer 2, please enter your name: ";
gets(player_2);
cout << endl << player_1 << " you are X\n";
cout << endl << player_2 << " you are O\n\n";
In codewarrior this works fine, but in dev c++ the output looks like this:
Player 1, please enter your name:
Player 2, please enter your name: joe
you are X
joe you are O
Press any key to continue . . .
it skips inputting the first players name, any body know why? I really appreciate any help... got to finish this by monday
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-01-18
On other GNU implementations I have seen the same problem with gets(). It seems to be down to a newline character from a previous operation being left in the keyboard buffer and terminating the gets() early. If it does it consistently, simply add a dummy gets() call (nasty non-portable fix). Or get technical and flush the keyboard buffer.
As a coding style issue: you are mixing C++ style iostream calls (cin) with C style stdio calls (gets). I suggest using iostream throughout - this may also have the side effect of solving your problem - by avoiding it altogether.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
At school I am taking a c++ class, and in school we use codewarrior for the mac. At home tho I use Dev c++ (much better than codewarrior).
My problem occurs when I try to input to strings right after each other:
cout << "\nPlayer 1, please enter your name: ";
gets(player_1);
cout << "\nPlayer 2, please enter your name: ";
gets(player_2);
cout << endl << player_1 << " you are X\n";
cout << endl << player_2 << " you are O\n\n";
In codewarrior this works fine, but in dev c++ the output looks like this:
Player 1, please enter your name:
Player 2, please enter your name: joe
you are X
joe you are O
Press any key to continue . . .
it skips inputting the first players name, any body know why? I really appreciate any help... got to finish this by monday
instead of gets use
cin.getline(player_1, 25, '\n');
&
cin.getline(player_2, 25, '\n');
On other GNU implementations I have seen the same problem with gets(). It seems to be down to a newline character from a previous operation being left in the keyboard buffer and terminating the gets() early. If it does it consistently, simply add a dummy gets() call (nasty non-portable fix). Or get technical and flush the keyboard buffer.
As a coding style issue: you are mixing C++ style iostream calls (cin) with C style stdio calls (gets). I suggest using iostream throughout - this may also have the side effect of solving your problem - by avoiding it altogether.