(Y/N)?

Hi again :)
As you may have read, I'm working on a text-based C++ game for the console (is that what its called?), and I have points where the user chooses an option. What i want to add is the option for the user to say Yes or No to their choice, before the program goes any further. I saw someone use a do-while loop, but their explanation confused me. Any tips?
thx
Last edited on
what you can do is:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool getInput()
{
char input  = x;
while ((input != 'y') || (input != 'n'))
{
cin >> input;
cin.ignore();
}
if (input == 'y')
return true;
else
return false;
}

hey thx, thats great...but could you maybe explain what you did or show me a tutorial on this? I wouldn't want to just copy it, i wouldn't learn anything
http://www.cplusplus.com/forum/articles/28558/
Generally, you will not learn anything from this in the first place. Console game programming is a complicated, frightening branch of programming that takes a lot of time, tends to be quite a mess to program and document and has a result that many would frown upon.

As of the current issue, I would recommend you to check these links out:
http://www.cplusplus.com/doc/tutorial/
http://xoax.net/comp/cpp/console/
1
2
3
4
5
6
7
8
9
10
11
bool getInput()
{
    char input; //Create a type to put 'y' or 'n' inside
    while ((input != 'y') && (input != 'n')) //Loops until input is 'y' or 'n'
        cin >> input;

    if (input == 'y') //if it's 'y' return true
        return true;
    else
        return false;
}


Now to use it...

1
2
3
4
5
6
7
//code
cout << "Would you like to buy an apple?<y> or <n>\n> ";
if (getInput) //if it's true
   cout << "\nHere's your apple\n";
else
   cout << "Fine then...\n";
//code
Would you like to buy an apple?<y> or <n>
> y
Here's your apple
Last edited on
this is great :) thx so much!
one last question...how could i make it so that if the user chooses 'no' it goes back to the question instead of displaying a new line of text?

@Kyon; You're right lol, i didn't realize what i was getting into. However, I'd like to try and stick it out and finish it. I'm slightly more advanced than the typical beginner, what do you recommend that I work on?
More on idiot-proof yes/no input:
http://www.cplusplus.com/forum/beginner/18518/
Both the terms advanced and beginner are arbitrary. IO in this sense is a very simple subject, going through the creation of a game would require you to (at least) know about the STL containers vector and list and using Object-Oriented Programming (aggregation, inheritance, polymorphism, friends, virtual members, static members, et cetera) as well as having a solid foundation of the language's constructs and operators.
Thank you both.
Topic archived. No new replies allowed.