If Statements

Hi there,
I am writing a program and I need input from it. But I only want to receive input if it is either 'y' or 'n'. I tried writing an if statement that checks the output like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
while (check_steam != 'y' || 'n')
    {
      cout << "That is not a valid answer.  Please try again.";
      cout << "\n> ";
      cin >> check_steam;
    }
    if (check_steam == 'y')
    {
      ofstream myfile;
      myfile.open ("config.mlu", ios::out | ios::app);
      myfile << "yes\n";
      myfile.close();
      default_path = "C:\\Program Files\\Steam\\steamapps\\common\\Star Wars Republic Commando\\";
    }
    if (check_steam == 'n')
    {
      ofstream myfile;
      myfile.open ("config.mlu", ios::out | ios::app);
      myfile << "no\n";
      myfile.close();
      default_path = "C:\\Program Files\\LucasArts\\Star Wars Republic Commando\\";
    }
    

With that code it comes up with the error message and works if you type in 'y' but not 'n'.

So I need some code to pick up input and ask the user to try again if it isn't correct.

I have heard about try() throw() and catch() and I tried reading the tutorial on them but it just confused me so I didn't know if thats what I needed to look for.
Last edited on
while (check_steam != 'y' || 'n')

The || operator evaluates the left and right side of it. If either value is true (nonzero) the result is true.

Basically what you have there is the same as this:

while( (check_stream != 'y') || ('n' != 0) )

Which obviously is not what you want. Since 'n' is never zero, the condition will always be true and the while loop will run forever.




You need to do this:
while(check_stream != 'y' || check_stream != 'n')


EDIT: that's wrong -- see hamsterman's post
Last edited on
it's
1
2
3
while (check_steam != 'y' && check_steam != 'n'){
   cin >> c;
}

or
1
2
3
4
5
6
7
8
9
10
while(1){
   cin >> c;
   if(c == 'y'){
   }else if(c == 'n'){
   }else{
      //error message
      continue;
   }
   break;
}

by the way, if you choose the first way, you must either initialize check_steam to 0, or use do cin >> c; while(c != 'y' && c != 'n'); to prevent random errors.
The second way worked thanks.
it would make sense to initialize check_steam to a char because that's what he's checking... just say, but yes in the statement it doesn't know what your supposed to be checking 'n' with. I think if you were to use the || method it might cause an infinite loop for some reason, cuz whenever i've tried using || in a while loop it throws it into a infinite loop, i'm not sure why though.
Of course it loops for ever. It loops until c != 'y' and c!='n' both return false, but when c == 'y', c !='n' and vice versa. The loop with || would only stop if c could be 'y' and 'n' at the same time.
yeah, i see that. When I tried it, i said something like:
while( c != 'y' || c! = 'Y' && c != 'n' || c != 'N' )

it would have the same issue that hamsterman was suggesting right?
Those or logic operators are killing you. You need to say while ((c != ' y') && (c != 'n')).
I'd also make sure anticipate the user entering capital letters.
while( c != 'y' || c! = 'Y' && c != 'n' || c != 'N' )
int !=, ! and = cannot be separated by a gap.
&& has higher precedence that || so what you wrote is equivalent to while( c != 'y' || (c != 'Y' && c != 'n') || c != 'N' ) which would work when c is 'Y' of ' n', but not 'y' or 'N'. If you add parentheses where they should be (while( (c != 'y' || c != 'Y') && (c != 'n' || c != 'N') ) it should work fine.
oh ok, thx for the clarification. Those damn parentheses always throw things off, lol.
Topic archived. No new replies allowed.