• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

user input problem in beginning c++ course...

wildwolf

Golden Member
I've run into a dilema when requesting a response from a user, and can't
figure out the proper method to use to get the results I am after.

What I have is a basic question asking two users if they wish to continue,
and a cin statement that gathers their response.
What kills the process I have, is I'd like to specifically make each user
enter only a lowercase 'y' or 'n' as their response. What I have works, but
it will accept any input from the user. Thus, it 'breaks' the program if a
user types "yes" or "no" or other non-revelant characters. I've tried it
with string, char, and combinations of int, but can't get anything working
properly. Would you be so kind as to point me in the right direction?

Here's the relevant code that I've managed to get working as long as they
type in only a 'y' or an 'n':
char playAgain;
cout << playerStats[playerTurn].playerName << " has won!" << endl;
playerStats[playerTurn].wins++; //Add one to player's total
wins.
cout << playerStats[1].playerName << ", would you like to play again
(y/n)? ";
cin >> playAgain;
if (playAgain == 'n')
finished = 1;
cout << playerStats[2].playerName << ", would you like to play again
(y/n)? ";
cin >> playAgain;
if (playAgain == 'n')
finished = 1;
if (finished != 1)
{
......
}


I did use: string playAgain; at one point, and was using: if (playAgain[0]
== 'n') { ..... } and while that would match the first character quite
nicely...any extraneous characters were taken as input for the 2nd
user...thus they weren't given a chance to respond.

Sorry about the bad formatting, don't know how to get it to show up correctly...any thoughts?
 
Hi wildwolf,
first, I assume finished is initialized at 0. If I understand correctly what you're trying to do, you want the game to stop if either player decides to quit, right?
I'd try that:

playAgain = 0;
while(playAgain != 'y' || playAgain != 'n')
{
cout << playerStats[1].playerName << ", would you like to play again (y/n)? ";
playAgain = getchar();
}

if (playAgain == 'n')
{
finished = 1;
}
else
{
playAgain = 0;
while(playAgain != 'y' || playAgain != 'n')
{
cout << playerStats[2].playerName << ", would you like to play again (y/n)? ";
playAgain = getchar();
if (playAgain == 'n')
finished = 1;
}
}
if (finished != 1)
{
......
}

you'll need to include stdio.h for the getchar() to work. This function returns the next character the user types in (so the player won't have to hit 'y' Enter, he'll just do 'y'. If the player is a moron and answers, say, 'a' instead of y/n, the while loop will ask him again if he wants to play.
Tell me how it turns out!
 
Originally posted by: ElDonAntonio
oh, so what's getch() ?

It's a C function that returns exactly one character from the keyboard WITHOUT waiting for the user to hit the Enter key. No buffering is done and raw input can be processed by the program. Looking at the documentation, I guess the proper form is _getch() for Windows/VC++.
 
I may be thick here, but what's the difference with getchar() then? you mentioned getch() was better than anything else. Just wondering.
 
Originally posted by: ElDonAntonio
I may be thick here, but what's the difference with getchar() then? you mentioned getch() was better than anything else. Just wondering.

getchar() will buffer the line.. you have to hit *Enter* before a char will be returned to you.
 
Originally posted by: ElDonAntonio
oops, I thought getchar didn't need the enter key...but I checked and you're right. Thanks for the clarification singh!

That's good, because I was getting tired of explaining 😀😉
 
you? tired of explaining? nahhhh, you're the 24/7 programming-guru-help-line around here! 😀

Hey btw, thanks to your help, my programming project is advancing quite nicely, you can check it out here (but you'll need Windows...sorry about that 🙂 )
Tell me what you think!!
wildwolf, sorry about the off-topic post, but at least I bumped your thread a few times 🙂
 
Originally posted by: ElDonAntonio
you? tired of explaining? nahhhh, you're the 24/7 programming-guru-help-line around here! 😀

Hey btw, thanks to your help, my programming project is advancing quite nicely, you can check it out here (but you'll need Windows...sorry about that 🙂 )
Tell me what you think!!
wildwolf, sorry about the off-topic post, but at least I bumped your thread a few times 🙂


Looking good 😎 Keep us updated 🙂
 
What I have works as far as stopping/continuing when needed. My problem is that if a user enters something _other_ than a 'y' or a 'n' then it breaks the program. Oh well, have to have it turned in in about 4 hours...looks like what I have is what the teacher's getting. 🙂

Thanks for the input folks.

 
Ok, correct me if I'm wrong here....I've been looking at code for too long on this. 🙂

But, your code looks like this:

playAgain = 0;
while(playAgain != 'y' || playAgain != 'n')
{
cout << playerStats[1].playerName << ", would you like to play again (y/n)? ";
playAgain = getchar();
}


Right? If those are the only changes you made, then, it does not work for me. In that instance, no matter what is input from the keyboard in response to the answer, it continually asks the question (ie: never exits the while loop). Note, it stayed in the loop even if I typed n or y as my response.

Yes, you did have it right. I wanted the whole program to exit if either player wanted to quit. What I need, I think, is a means to accept input from the keyboard, and have it clear the buffer from after the first character, so that there's a clear buffer for the next person to answer with their own response. (rather then characters waiting in the buffer being their response...)

That help explain where I'm stuck?

Btw, program already turned in for a grade...so it wont help me achieve a better grade or anything, but it will help me learn what the heck needs to be done for future reference.
 
Hi wildwolf,

yep I had a little error in my code, I confused the || with && in the while loops. Well I tried the code for real this time and I think I have what you want. Try this:

#include <stdio.h>
#include <iostream.h>

void main()
{
char playAgain = 0;
int finished = 0;
while(playAgain != 'y' && playAgain != 'n')
{
fflush(stdin);
cerr << " would you like to play again (1) (y/n)? \n";
playAgain = getchar();
}

if (playAgain == 'n')
{
finished = 1;
}
else
{
playAgain = 0;
while(playAgain != 'y' && playAgain != 'n')
{
fflush(stdin);
cerr << "would you like to play again (2) (y/n)? \n";
playAgain = getchar();
if (playAgain == 'n')
finished = 1;
}
}

if (finished == 1)
{
cout<<"finished"<<endl;
}
}

the fflush(stdin) will take care of wiping the useless extra caracters.
 
Originally posted by: ElDonAntonio
Hi wildwolf,

yep I had a little error in my code, I confused the || with && in the while loops. Well I tried the code for real this time and I think I have what you want. Try this:

#include <stdio.h>
#include <iostream.h>

void main()
{
char playAgain = 0;
int finished = 0;
while(playAgain != 'y' && playAgain != 'n')
{
fflush(stdin);
cerr << " would you like to play again (1) (y/n)? \n";
playAgain = getchar();
}

if (playAgain == 'n')
{
finished = 1;
}
else
{
playAgain = 0;
while(playAgain != 'y' && playAgain != 'n')
{
fflush(stdin);
cerr << "would you like to play again (2) (y/n)? \n";
playAgain = getchar();
if (playAgain == 'n')
finished = 1;
}
}

if (finished == 1)
{
cout<<"finished"<<endl;
}
}

the fflush(stdin) will take care of wiping the useless extra caracters.

I took C++ last year, little bit rusty on some of the stuff, but here's my attempt without using stdio.h.

#include <iostream.h>
#include <conio.h>

void main () {
char playAgain = 0;
int finishedplayer1 = 0;
int finishedplayer2 = 0;
cout << "Would you like to play again? (1) (y/n)";
cin >> playAgain;
while ( (playAgain != 'y') && (playAgain != 'n')) {
cout << "Please enter either 'y' for yes or 'n' for no, nothing more, nothing less, you ignorant fool.";
cin >> playAgain;
}
if (playAgain == 'n')
finishedplayer1 = 1;
cout << "Would you like to play again? (2) (y/n)";
cin >> playAgain;
while ( (playAgain != 'y') && (playAgain != 'n')) {
cout << "Please enter either 'y' for yes or 'n' for no, nothing more, nothing less, you ignorant fool.";
cin >> playAgain;
}
if (playAgain == 'n')
finishedplayer1 = 1;


 
Originally posted by: BigJ2078

while ( (playAgain != 'y') && (playAgain != 'n')) {
cout << "Please enter either 'y' for yes or 'n' for no, nothing more, nothing less, you ignorant fool.";
cin >> playAgain;
}
ROTFL
Now THAT'S user-friendly!!! 😀 😀 😀
 
Originally posted by: ElDonAntonio
Originally posted by: BigJ2078

while ( (playAgain != 'y') && (playAgain != 'n')) {
cout << "Please enter either 'y' for yes or 'n' for no, nothing more, nothing less, you ignorant fool.";
cin >> playAgain;
}
ROTFL
Now THAT'S user-friendly!!! 😀 😀 😀

He's probably a *nix programmer 😉
 
yeah just make a while loop that does not go out of scope until char "y" || "Y" || "n" || "N" is entered. thats the way i always did it
 
The problem with the while loop & just a char for 'y' or 'n' is that it would alter the input for the 2nd user...at least in my actual program it would. What I eventually got to work, was make an array of char, with a length of 2 (one for the y or n, and one place for the /n charater) and then did a strcmp for y, n, and also a strlen != 1 in a while loop, like so:

{
char playAgain[2];
cout << playerStats[playerTurn].playerName << " has won!" << endl;
playerStats[playerTurn].wins++; //Add one to player's total
wins.
cout << playerStats[1].playerName << ", would you like to play again
(y/n)? " << endl;
cin >> playAgain;
while ((strcmp(playAgain,"n") != 0) && (strcmp(playAgain,"y") != 0)
&& (strlen(playAgain) != 1))
{
cout << "Please enter y or n to continue: " << endl;
cin >> playAgain;
}
if (strcmp(playAgain,"n") == 0)
finished = 1;
cout << playerStats[2].playerName << ", would you like to play again
(y/n)? " << endl;
cin >> playAgain;
while ((strcmp(playAgain,"n") != 0) && (strcmp(playAgain,"y") != 0)
&& (strlen(playAgain) != 1))
{
cout << "Please enter y or n to continue: " << endl;
cin >> playAgain;
}
if (strcmp(playAgain,"n") == 0)
finished = 1;
if (finished != 1)
{
......
......

}
}
}


Btw, is there any way to get this msg board to format code with indentation, etc as the user wants?

 
Yes, I tried your code. Didn't work.

This is what I tried:
int main()
{
char playAgain = 0;
int finished = 0;
while(playAgain != 'y' && playAgain != 'n')
{
fflush(stdin);
cerr << " would you like to play again (1) (y/n)? \n";
playAgain = getchar();
}

if (playAgain == 'n')
{
finished = 1;
}
else
{
playAgain = 0;
while(playAgain != 'y' && playAgain != 'n')
{
fflush(stdin);
cerr << "would you like to play again (2) (y/n)? \n";
playAgain = getchar();
if (playAgain == 'n')
finished = 1;
}
}

if (finished == 1)
{
cout<<"finished"<<endl;
}
return 0;
}


These are runs of it:

117 frank% ./prog1b
would you like to play again (1) (y/n)?
nope
finished
118 frank% ./prog1b
would you like to play again (1) (y/n)?
n
finished
119 frank% ./prog1b
would you like to play again (1) (y/n)?
y
would you like to play again (2) (y/n)?
nope
finished
120 frank% ./prog1b
would you like to play again (1) (y/n)?
yes
would you like to play again (2) (y/n)?
yes
121 frank%

When I'd type "yes" "nope" it would interpret those as a "y" or "n" which I did not want it to do.
 
if you do what I stated in a while loop with string instead of char it should work. There is no reason why it would mess up the 2nd users input if written correctly.
 
Back
Top