简体   繁体   中英

if.. else.. statements not outputting expected results. While.. loop issue?

Note that choiceOne_One is equal to 0 before user input.
I am having issues with my current code. I would like to have the user be able to just retype their input until it is valid instead of having to close the program and re-open it. Because of this (and from the advice of others on the interwebs), I have added a while loop, which seems solid. For some reason, when the user inputs 1, it loops through the first if statement as if the input was invalid, outputting "Woah! That wasn't an option.. etc.". I'm not sure how to make this work, or even what is wrong with it. Any help would be much appreciated.

cin >> choiceOne_One;
    int whileInt=0;

while(whileInt == 0)
{

if (choiceOne_One != 1 || 2)
    {
        cout << "Woah! That wasn't an option! Try Again.\n";
    }

else if (choiceOne_One == 1)
    {   
        whileInt++;
        cout << "One\n";
    }


else if (choiceOne_One == 2)
    {
        whileInt++;
        cout << "Two\n";
    }
}

I believe it's because of your if statement. You should change it to

if (choiceOne_One != 1 && choiceOne_One != 2)

This is because the 'or' statement is not correctly used in your current code. By having

(choiceOne_One != 1 || 2)

you are essentially saying,

  • if (choiceOne_One !=1)

OR

  • if (2).

The latter statement is always true, because in C++, if you have an if statement followed by an integer, it will simply return the boolean "true".

This website here tells you more about how that works. http://www.cplusplus.com/forum/articles/3483/

Hope that helps :)

The problem is caused because choiceOne_One != 1 || 2 choiceOne_One != 1 || 2 is parsed as (choiceOne_One != 1) || 2 (choiceOne_One != 1) || 2 which is always accepted by the conditional (because 2 is a "true" value independent of the choice).

The way [I recommend] to "fix" this code is use an else and not to change the conditional, although such would also work. Consider;

if (choiceOne_One == 1)
    {   
        whileInt++;
        cout << "One\n";
    }       
else if (choiceOne_One == 2)
    {
        whileInt++;
        cout << "Two\n";
    }
else // only here if NONE of the above conditions are true
    {
        cout << "Woah! That wasn't an option! Try Again.\n";
    }

Also,

  1. Read a input value inside the loop. Currently choiceOne_One will never change while the shown loop is running - this will result in a loop that terminates immediately or never terminates.
  2. whileInt could/should be changed or eliminated - either use a boolean flag or a break/return.

You have to complete your if statement:

if (choiceOne_One != 1 && choiceOne_One != 2)
   {
    cout << "Woah! That wasn't an option! Try Again.\n";
   }

double || means or and && means and so both conditions need to be satisfied for it to print

The problem is the line if(choiceOne_One != 1 || 2)

You have to expand this to if(choiceOne_One != 1 && choiceOne_One != 2)

The reason is that in C++, integers evaluate to true if they are nonzero. So with your original statement, choiceOne_One != 1 || 2 choiceOne_One != 1 || 2 becomes choiceOne_One != 1 || true choiceOne_One != 1 || true , and of course anything OR'ed with true is true .

cin >> choiceOne_One;
while(true)
{
if (choiceOne_One != 1 && choiceOne_One != 2)
{   
    cout << "Woah! That wasn't an option! Try Again.\n";
    cin >> choiceOne_One;
}else if(choiceOne_One == 1){

    cout << "One\n";
    break;

}else if (choiceOne_One == 2){
 cout << "Two\n";
    break; 
 }
}

Sorry i don't have c++ compiler to test this code but i think this will work with you

After editing the first if() condition like some of you have pointed out, I realized I was still having the problem of the program indefinitely looping through the first statement. I also realized that this is because I didn't write a line giving the user another chance to input something valid (it felt good to realize this). So, I expertly cut and pasted the line "cin >> choiceOne_One;" from outside the while() loop into the while() loop and voila! it worked. In retrospect, I should have noticed this sooner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM