Does Not Equal in relation to multiple possible inputs

Hi everyone! I am new to the forum. Before I start, I will say I have googled this question, checked the forum, and struggled with it on my own for a couple days not. It may be something I have missed which is fairly simple (and I hope it is!) but I can't think of what to do next. I'd rather look like an idiot and get the code to work than keep running circles and learn a thousand ways NOT to do it.

The problem is, I am trying to take an input line where the user can put in either a or b, for instance, and spit out a statement and re-ask the question if the input is not a, b, A,or B.

I found another thread asking the exact same question but it didn't get answered. The part where the OP reiterated that the code worked if it was only 1 possible correct response but not when there were multiple correct responses, got ignored. I will post a basic bit of code for what I am trying to do and hopefully it makes sense.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<string>

using namespace std;

int main ()
{
   string letter;
   cout << "input a or b ";
   cin >> letter;

   if (letter != "a" || "b"){
       cout << "no!!!!" << endl;
   }
   else
       cout << "yes!!!" << endl;
return(0);
}


Full disclosure, this is part of a larger program which is a school assignment. I am working ahead of my class and am pretty good at figuring this stuff out. I have tried multiple variations and ways of accomplishing this task. Ultimately, this will be in a bool statement to loop the question if a false answer is given or to continue the code if a correct response is inputted. Any help that could be given here would be amazing.

Thanks in advance!
-Arcie
Just tweaked the logic in the if statement a bit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<string>

using namespace std;

int main ()
{
   string letter;
   cout << "input a or b ";
   cin >> letter;

   if (letter != "a" && letter!= "b"){
       cout << "no!!!!" << endl;
   }
   else
       cout << "yes!!!" << endl;
return(0);
}


or this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<string>

using namespace std;

int main ()
{
   string letter;
   cout << "input a or b ";
   cin >> letter;

   if (!(letter == "a" || letter == "b")){
       cout << "no!!!!" << endl;
   }
   else
       cout << "yes!!!" << endl;
return(0);
}



DeMorgan's laws can be helpful in figuring out logical ANDs and ORs.
http://en.wikipedia.org/wiki/De_Morgan's_laws
Last edited on
Okay, let's do this then. :-)

First off, I'm going to change the string to a char, since we're only interested in one letter.

There are a couple of issues with your or condition. The compiler sees the second as a string literal (a const char* containing the letter b), which will always evaluate to true in an if statement. We need to explicitly state the condition.
 
if (letter != "a" || letter != "b")


Have a bit of a think about that statement, though. If the letter isn't 'a' OR if the letter isn't 'b'. Well, if the the letter is 'a', then it isn't 'b' and vice versa. In short, this if statement is always going to evaluate to true. What we want is:
 
if (letter != 'a' && letter != 'b')


You're right about the loop, so let's get one in there.
1
2
3
4
5
while (letter != 'a' && letter != 'b')
{
   std::cout << "Incorrect letter, enter a or b:";
   std::cin >> letter;
}


What you'll often notice with these sorts of input loops is that it can go a bit crazy if someone enters the wrong input and could do things like spit out the cout statements in the loop multiple times. For this reason, it's a good reason to tell std::cin to ignore whatever is remaining in the buffer.
1
2
3
4
5
6
while (letter != 'a' && letter != 'b')
{
   std::cout << "Incorrect letter, enter a or b:";
   std::cin.ignore(256, '\n');
   std::cin >> letter;
}

The arguments here basically tell it to to extract and ignore 256 characters or up until it finds a newline.

Another thing we can do is check that the input to the std::cin stream is as expected. Explaining this one might be a bit overkill right now, but you can do it by adding this to your loop conditions.
1
2
3
4
5
while ( !(std::cin >> letter) || (letter != 'a' && letter != 'b'))
{
   std::cout << "Incorrect letter, enter a or b:";
   std::cin.ignore(256, '\n');
}

Note how we no longer need to get our input in the loop; we're actually doing it in the condition.

Finally, we need to cater for uppercase characters. A and B are legitimate entries, so we need to allow them. We could add two more conditions in our loop but there's an easier way. C++ providers a tolower function, which is available from the cctype header (http://www.cplusplus.com/reference/cctype/tolower/). We can make use of that to allow uppercase characters. This makes our final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cctype>

int main(int argc, const char* argv[])
{   
    char letter;

    std::cout << "Enter a or b:";

    while ( !(std::cin >> letter ) || 
             (std::tolower(letter) != 'a' && std::tolower(letter) != 'b'))
    {
        std::cout << "Incorrect letter, please enter a or b:";
        std::cin.ignore(256, '\n');
    }

    std::cout << "Congratulations, you entered " << letter << std::endl;
    
    return 0;
}


Hope this helps. :-)

Edit: Yikes, I started writing this, went to get something to eat and then continued afterwards. I didn't realise someone had replied in that time. :-)
Last edited on
Ooh, I really like the:

if (!(letter == "a" || letter == "b"))

line a lot! Hadn't thought about putting the false value in front of the whole line. Could a bool statement be put in the "if" line to loop the program to the question?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main ()
{
   bool start = false;
   while ( !start ) {
      string letter;
      cout << "input a or b ";
      cin >> letter;

      if (!(letter == "a" || letter == "b")){
          cout << "no!!!!" << endl;
      }
      else
      {
               cout << "yes!!!" << endl;
               start = true;
      }
     return(0);
}


Testing this as I post it. Thank you soooooo much for the response! You rock! Going to read that wiki entry on de morgan's laws.

-Arcie
@iHutch105, I posted a reply to wildblue and by the time I hit post, you had already responded! So it happened to me as well!!!

Thanks for that response. The info about the tolower library is going to help immensely. I will do some looking into that later just to verify the syntax (and learn it rather than copy/paste, haha).

In the loop you put into the condition, what if an invalid input is returned there? Will it loop back to the if/else statement? Sorry if that sounds like a noob question!

You guys are awesome. I am going to play with this and see if I can get my stuff to work!

-Arcie
Yep, if someone enters something other than 'a', 'A', 'b' or 'B' then it'll just ask them for another input.
Thanks again! You guys rock!

-Arcie
Topic archived. No new replies allowed.