c++getchar

using getchar() function to pause program but something's wrong with it


I tried to using getchar() function to pause program when user enters 'm' or 'M'.

The main problem is I don't know why I can't use :

while (check != 'm' || check != 'M' );

Instead of:

while (check != 'm' && check != 'M' );

My code:

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int month, check;
    
    do
    {
        cout << "Enter the number, press 'm' or 'M' to exit. \n";
        cin >> month;

        check = getchar();
        switch(month)
        {
            case 1:
            case 2:
            case 12:
            {
                cout << "Winter. \n";
                break;
            }
        }
    }
    while (check != 'm' && check != 'M' ); // It works
    while (check != 'm' || check != 'M' ); // It doesn't

    return 0; 

}

I'm newbie so pls help me.


Solution

  • while (check != 'm' || check != 'M' );
    

    is nonsense because the condition will always be true assuming that 'm' and 'M' have different values.

    When check equals to 'm', it is not equal to 'M', so check != 'M' is true and the condition is true.

    When check doesn't equal to 'm', check != 'm' is true, so the condition is true.