Error checking, stuck in loop

Aug 17, 2012 at 2:41am
Hey Everyone,

I don't understand why I'm stuck in a while-loop when i'm trying to error check. It's line 13-17 where I'm stuck in the error checking loop.

Any advice? Thanks in Advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    Calculator x;
    double op1, op2;
    char op;
    char rep;
    do {
        cout << ">";
        cin >> op1 >> op >> op2;
        cout << x.calc(op1, op2, op) << endl;
        cout << "Again (y/n)? ";
        cin >> rep;
        while (rep != 'n'|| rep != 'y') {
            cout << "Error\n";
            cout << "Another (y/n)? ";
            cin >> rep;
        }
    } while(rep != 'n');
    return 0;
} 
Aug 17, 2012 at 2:49am
This statement is always true:

rep != 'n'|| rep != 'y'
Aug 17, 2012 at 3:18am
Why is that condition always true?, if you don't mind my newbie question.

If rep == n, it should break.

My intention is to check for invalid entries other than y and n...




Last edited on Aug 17, 2012 at 3:50am
Aug 17, 2012 at 5:15am
I figured it out! I will post my solution.

If there is a more efficient way of doing this, please let me know.

Thanks for the help!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    Calculator x;
    double op1, op2;
    char op;
    char rep;
    do {
        cout << ">";
        cin >> op1 >> op >> op2;
        cout << x.calc(op1, op2, op) << endl;
        cout << "Again (y/n)? ";
        cin >> rep;
        if (rep != 'n' && rep != 'y')
            do  {
            cout << "Error\n";
            cout << "Again (y/n)? ";
            cin >> rep;
            }
         while (rep != 'n' && rep != 'y');
    } while(rep != 'n');
    return 0;
}

Aug 17, 2012 at 5:18am
You are telling it to do the following when rep does not equal 'n' OR when rep does not equal 'y'. Basically, unless it equals both, the following will execute. Do while (rep != 'n' && rep != 'y') .
Topic archived. No new replies allowed.