Variations of Yes/No Input from Users

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.
You can try all the blind test you want. Why not just look it up ?

Try
1
2
if ((input == "Yes") ||(input == "yes")||(input == "Y")||(input == "y"))
{// do something} 


Perfect. I like that - it's very simple and easy to follow. Thank you!

Out of curiosity though, why would the compiler allow this to be compiled with no errors or warnings?

1
2
3

if(input == "yes" or "Yes" or "y" or "Y")


This almost leads me to believe that there are instances where you can assign multiple parameters to the if (input == "x"); function. I'm not sure if my logic is correct there, or even if I'm framing the question right.
Don't know but it didn't work for me.

1
2
3
4
5
6
7
8
9
10
int main (int argc, char *argv[])
{
string input="N";
if(input == "yes" or "Yes" or "y" or "Y")
{
	cout << "." << input << ".";
}
return 0;
}




C:\Temp>new
.N.
Last edited on
This doesn't mean what you think it does,
if(input == "yes" or "Yes" or "y" or "Y")

To understand why, you need to understand that the number 0 or zero can be interpreted as false. Any non-zero value will be interpreted as true.
thus it has the same meaning as
if((input == "yes") or true or true or true)
which simplifies to
if (true)
that is to say, no matter what the user types, the condition as a whole is always true.
Topic archived. No new replies allowed.