WHILE LOOP fails to exit

I have run the bit of code below with a single digit as the exist condition, and it works, and have since been trying a number of things to make it work and I do not know why. It was originally a while loop, but i most recently tried to do a do while. 6,7,8, and 9 are all direction codes for a file system that I am making. If it helps, direction is declared in private variables in the same class as this code (this part of a function of that class). Strangely, it even writes correct number in with the cin>>. Please help :)

1
2
3
4
5
6
7
  do
            {
                cout << "Enter 6, 7, 8, or 9" << endl;
                cin >> direction;
                cout << "Direction = " << direction << endl;

            }while(direction != 6||7||8||9);
}while(direction != 6||7||8||9);
this is incorrect


while direction != 6 or direction != 7, etc.
Last edited on
I have done it in that format as well. so that it would appear as
1
2
3
4
while(((direction != 6)||(direction !=7))||((direction != 8)||(direction !=9)))
{
//above loop
}
If you want to exit from the loop use the && operator instead of ||
because the number director never gonna equal 6,7,8 and 9 at the same time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include <iostream>
using namespace std;

int main()
{
    int direction = 0;

    while((direction != 6&&direction !=7)&&(direction != 8&&direction !=9))
    {
        cout << "Enter 6, 7, 8, or 9" << endl;
        cin >> direction;
        cout << "Direction = " << direction << endl;
    }
}
Last edited on
Oh.. your right. Thank you very much. For some reason I could not see what the problem was with my code. I see that 3 will always be true and there for all of the ORs will be true. Thank you.
Topic archived. No new replies allowed.