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

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
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
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.

You said char first, then string in your last post.

I tried it with both plain char and a plain string. Neither would work properly. Either one would give an error in the logic some way or another. I tried different variations of char, string, cin.get cin.ignore, and a bunch of other stuff...what I posted in my last bit of code is all I managed to get to get work exactly as I stated: It will ask for a y/n first, then anytime anything other than a y or n is input, it'll reask for the input and specify y or n again. It'll repeat process for the 2nd user. So far, has worked every time I've tried it this method, none others would work for all tests.

Tests I used were to enter the following:

yes, sure, okay, whatever, nope, no way, y, (and a y or n as the last one, first y, then repeat process for an n).
 

mobogasm

Golden Member
Oct 25, 1999
1,033
0
0
i know that is why i restated myself in 2nd post saying that if u use idea in first post with string instead of char it should work. i'm sure yours works fine, and that's all that matters. I just never had a problem in 2 years of c++ using my method. But there are so many methods you can use just like for every problem in programming. later.
 

Surg187

Member
Feb 5, 2001
67
0
0
Not sure if you need anymore help but I used the string class here. If you use Visual C++ 6.0 there is an error in the string class where getline reads an extra character. You can fix it if you change the string header file. A description of the error is here. You can change finished to an int if you would like. I tried this quickly and it seems to work.

#include <iostream>
#include <string>
using namespace std;

void main()
{
string playAgain = "";
bool finished = false;
while ( playAgain != "y" && playAgain != "n" )
{
cout << "Player 1 would you like to play again?" << endl;
getline(cin, playAgain);
}
if ( playAgain == "n" )
finished = true;
else
{
playAgain = "";
while ( playAgain != "y" && playAgain != "n" )
{
cout << "Player 2 would you like to play again?" << endl;
getline(cin, playAgain);
}
if ( playAgain == "n" )
finished = true;
}
if ( finished == true )
cout << "Finished" << endl;
else
cout << "Not finished" << endl;
}
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
mobogasm: "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."

That was my point entirely, I couldn't figure out how to get it written correctly. If you could shed the light on it for me, I'd appreciate it. Meant no offense, if some was taken. I'm the one learning here...and mind is swirling with all the things I've tried that didn't work, can't remember them all, but I spend the better part of a few hours trying different variations.

Btw, for any who care, these are being typed in vi, and compiled & ran on a unix machine at school, not visual c++....it that matters. I've only recently installed the visual studio, and still aren't sure how to compile once I write a source file. :)
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
wildwolf, that's pretty weird it doesn't work on unix, I've compiled and tested my code like you did on Windows (Visual C++) and it works like a charm.
Go figure!
 

Surg187

Member
Feb 5, 2001
67
0
0
Just ran the sample I posted and it works on Unix. By the way, in my code near the bottom, you can change

if ( finished == true)
to
if ( finished)

Well, hope this helps. It's good you are using Unix. Visual C++ is easy to pick up.

Good Luck :)
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
ElDonAntonio

I put your code in on my linux machine, and compiled & ran it. I ran it twice.

[wildwolf@awolf ~]$ g++ testplay.cc -o testplay
[wildwolf@awolf ~]$ ./testplay
would you like to play again (1) (y/n)?
no My comment here, notice how it allowed me to input "no" when I needed it to tell me to choose n or y
finished
[wildwolf@awolf ~]$ ./testplay
would you like to play again (1) (y/n)?
yes My comment here, notice how it allowed me to input "yes" when I needed it to tell me to choose n or y, as a result, for some reason it starts off asking player 2 the question 4 times....
would you like to play again (2) (y/n)?
would you like to play again (2) (y/n)?
would you like to play again (2) (y/n)?
would you like to play again (2) (y/n)?
sure
would you like to play again (2) (y/n)?
would you like to play again (2) (y/n)?
would you like to play again (2) (y/n)?
would you like to play again (2) (y/n)?
would you like to play again (2) (y/n)?
n
finished
[wildwolf@awolf ~]$

Just wasn't quite what I was after. Do not know why it works for you & not me, but it doesn't, on two different machines. (Unix at school, Linux at home).
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
Surg187,

I tried yours...and it works just fine...but makes me want to ask y'all some other questions now.

When it is required to use:
if (strcmp(playAgain, "y") != 0)
.....

vs.

if (playAgain != "y")
.....

ie: when I can I use the 2nd one, and not worry about strcmp? Use strcmp when the string's an array of char?

Also, does yours work differently because of the getline(cin, playAgain); line? What does the getline call do here that makes it work over the cin?
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
Hi wildwolf,

I looked it up and it seems that fflush is only clearly defined for output streams. It seems like the MSVC++ compiler will make it run fine with input streams (stdin), but that explains why it's not working on your *nix boxes.

An alternative: replace all the fflush(stdin) in my code with:

char c;
while ((c=getchar())!=EOF && c!='\n') ;

or with:

cin.ignore(200, "\n")

the first one is better, the second one is simpler (ignore 200 caracters or stop when you find \n).

As for your question, AFAIK, you should never use the second form. I've never worked with string types, I always use char arrays, so somebody else should be able to help you more than me.
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
ElDonAntonio:

I tried the cin.get & cin.ignore...and while I forget exactly what I used, I think it was similar to this:
int main()
{
char playAgain;
int finished;
cout << "Player 1, would you like to play again (y/n)? ";
cin.get(playAgain);
cin.ignore(200, '\n');
while (playAgain != 'n' && playAgain != 'y')
{
cout<< "Please enter y or n" <<endl;
cin.get(playAgain);
cin.ignore(200, '\n');
}
}
And it didn't quite work right either...that's when I went on to making the string an array of char, and finally got something that worked.

Hope that made sense...
 

Surg187

Member
Feb 5, 2001
67
0
0
Let me try my best to explain. This is off the top of my head so hopefully I can explain it correctly. The string class that I included simplifies the way strings are handled in C++. You probably haven't gotton to this part yet but C++ includes something called operator overloading. It just allows programmers to define operators for user-defined classes. The string class has overloaded the == operator and knows how to handle it. It just makes it easier to read. You can use the strcmp, but if using the string class it is easier to use ==. The getline function reads the input stream and puts it in the string. By default, it waits until the \n to stop reading, you can include a parameter to change that. I hope I haven't confused you. The main problem I have seen in your code is that you use a single char to store the answer. The first character typed is stored in the char and the rest ignored, but you don't want this. You want to make sure the user does type anything more than y or n. Here is a program using char array and strcmp. This is a slightly different getline here. The parameters are a bit different.

#include <iostream>
#include <cstring>
using namespace std;

void main()
{
char playAgain[100];
int finished = 0;
while ( strcmp(playAgain,"y") != 0 && strcmp(playAgain,"n") != 0)
{
cout<<"Player 1 do you want to play again?"<<endl;
cin.getline(playAgain,100);
}
if (strcmp(playAgain,"n") == 0)
finished = 1;
else
{
cout<<"Player 2 do you want to play again?"<<endl;
cin.getline(playAgain,100);
while ( strcmp(playAgain,"y") != 0 && strcmp(playAgain,"n") != 0)
{
cout<<"Player 2 do you want to play again?"<<endl;
cin.getline(playAgain,100);
}
if (strcmp(playAgain,"n") == 0)
finished = 1;
}
if (finished)
cout<<"Finished"<<endl;
else
cout<<"Not finished"<<endl;
}
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
Actually, in the code I wrote that solved my problem, I used an array of char to store the answer. Pretty much what you did. It's just I had tried your method earlier on my own..but couldn't get it to work..hence, why I finally went with trying the array of char method...which I got to work.

Thanks for responding..and, you didn't confuse me. :)