Help me understanding logical operator well

I've just created a c++ program that computes the sum of the cubes from 5^3 to N^3 and after the following datas were printed, the user will be asked if he/she wants to try the program again. If the user entered 1, the program will continue looping and if 2, the program will stop and if none of the following options was entered, he/she will be asked again if he/she want to retry and this is my first code I used for that.

1
2
3
4
5
6
1: do
2: {
3:     cout << "Do you want to retry?" << endl;
4:     cout << "1 - Yes" << endl << "2 - No" << endl;
5:     cin >> tryAgain;
6: }while(tryAgain != 1 || tryAgain != 2);


I've already fixed the line 6 into:

 
while(tryAgain < 1 || tryAgain > 2)


and Im pretty sure that I understand why that works but could you please explain why line 6 didn't work? Thanks ^_^
Because your loop will be executed againt if tryAgain is less than one (−∞;1) or larger than two (2;+∞). So only invalid numbers (when loop stops) are 1 and 2.
Try while(tryAgain != 1) It should work.
Well you want the program to loop if the user enters 1 correct? Well with your < 1 statement if you enter 1 it will still end the loop. Since 1 is not < 1. You can do <=1 to fix this.
I think it should be while( tryAgain != 1 && tryAgain != 2 );
since you seem to want it to ask the user again if they want to try again seems like this loop is inside of another while loop where you have something like
1
2
3
4
5
6
7
8
9
10
do
{
    //stuff
    do
    {
         //ask if they want to redo
     } while( tryAgain != 1 && tryAgain != 2 ); //if you use || it will always be
                                               //true...of course it will always not
                                               // be one of them
} while( tryAgain == 1 ); //repeat when try again is 1 


*forgot code tag.
Last edited on
thanks for helping me
Topic archived. No new replies allowed.