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
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.
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
returntrue;
elsereturnfalse;
}
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
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?
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.