The player is forced to answer y or n, i still think this code is larger than it has to be and that there are easier ways to do what I did but I just wanted to share with you guys…Notice that I played with the “for” loop and made ir work as a boolean XD.
Interesting.
I like this. You’ve effectively limited the selections a player can make in your code, but what happens when they enter something like “Don’t think so.”?
Looks like your code will just loop around again, regardless. I realize we haven’t really gotten into it but maybe make an else if statement that prints out something polite like “IT’S A YES OR NO QUESTION DOOFUS! YES. NO. THOSE ARE YOUR ONLY CHOICES, GEEZ!”
I know this is old, but I hope this helps others on their journey.
Here’s my own challenge to newcomers –
Take a look at the following code, and then research these concepts:
- Char
- Ternary Operator (Conditional Operator)
- “Can I return statements in c++?”
string Response = "";
getline(cin, Response);
char Ltr1 = Response[0];
bool TypedYes = (Ltr1 == 'Y' || Ltr1 == 'y');
bool TypedNo = (Ltr1 == 'N' || Ltr1 == 'n');
bool TypedYesOrNo = (TypedYes || TypedNo) ? true : false;
return TypedYesOrNo ? true : false;
Now, your first challenge is to tell me why the TypedYes and TypedNo is more readable, yet a bit excessively so. Some may not agree.
Your second challenge (extra credit), is to explain why the use of two ternary operators is kind of superfluous and unnecessary.
But your most important challenge is to learn what the char type is, and try to figure out how it related to the string type No worries if you can’t get that second part just yet.
Then, how might you condense this code into something that’s balanced between readability and concision? (In this case, but not all, the two are inversely related.)
And an extra! extra credit, what is a scenario where that readability might be more important than being concise? Hint: what if the boolean statement was checking many things, like whether a user typed a vowel or consonant?
Finally, why did I say, “what if the boolean statement was checking” and not, “what if the boolean variable” was checking?
Here’s a hint about what the 5th line does: “Light switch toggle equals - is the light switch toggle on? then turn it off. Else, if it’s already off, turn it on.” Notice the word, ‘else’.