Hello,
I am new to C++ programming - and even programming in general - so I am reading along in Bjarne Stroustrup's Programming Principles and Practices Using C++.
For the past two days I've been playing around with a snippet of code (below) to see what works, what doesn't, what makes it break, etc. After becoming comfortable with the simple constructs, I wanted to add various cases of "Yes" and "No" to the user's available input options so that multiple variations of the same response can return the appropriate responses. My way of adding complexity to it.
After a little Google magic, I found various examples of how to do it. Some simple, some more complicated, some that just don't make sense to me yet. Many of them were on this forum and others.
My first question is: based on my code below, what would the simplest way to implement "Yes," "yes," "y", and "Y" to my argument (would it be called an argument?) in order to anticipate variations of input? Note: after a bit of research, I came across a "toupper" function that I believe would be the simplest method, but I'm still tinkering with it to understand how to implement it. The definition leads me to believe it would only work for variations of "yes" and not simple inputs like "y."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <std_lib_facilities.h>
int main ()
{
string name;
string input;
std::cout << "Please, enter your name: ";
std::getline (std::cin,name);
std::cout << "Would you like to play a game, " << name << "?\n";
std::getline (std::cin,input);
if(input == "yes")
{
std::cout << "Excellent. What shall we play?\n";
}
else if(input == "no")
{
std::cout << "Are you sure?\nHow about a nice game of Global Thermonuclear War?";
}
else
std::cout << "Goodbye.";
return 0;
}
|
Just for experiment-sake, before finding the "toupper" function, I ran a series of blind tests to see what works/what doesn't. Here is one example of things I tried:
1 2 3
|
if(input == "yes", "Yes", "y", "Y")
|
And this was another blind test:
1 2 3
|
if(input == ("yes"), ("Yes"), ("y"), ("Y"))
|
Oddly, this compiles and runs just fine in some compilers and throws off just a warning in others. The warning is: "right operand of comma operator has no effect." However, there is some kind of logical or runtime error occurring, I believe. Whenever you run this code with the multiple yes parameter variations, no matter what you input (even non-yes-or-no responses) the program will not return the "Goodbye" specified in else. Instead, this will always return the output defined in the first if statement (again, no matter what you type - yes, no, fluffilufigus, doesn't matter):
1 2 3
|
std::cout << "Excellent. What shall we play?\n";
|
This I found a little odd, so it led me to a second question: Why would this be allowed to compile in the first place and why does it always call the same std::cout function no matter the input? I figured that if the right operands had no effect, then surely it would default to the first one ("yes") and non-yes-or-no responses would still proceed to call the else statement to return "Goodbye."
Another oddity:
The final alteration below compiles without error OR warning, but still produces the same logical error of displaying "Excellent..." no matter what you type.
Yet another blind test:
1 2 3
|
if(input == "yes" or "Yes" or "y" or "Y")
|
For reference, I'm using Code::Blocks IDE 13.12 with the TDM-GCC Compiler Suite for Windows w/ Standard MinGW 32-bit edition. I believe 4.7 series but not sure how to check the exact version of MinGW.