Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading