I'm using the latest VS C++ express edition for a basic intro to c++ class that is largely focused on numerical applications. Anywho, for whatever reason, I'm getting strange behavior from scanf("%c",&whatever). The program is supposed to use switch to perform some operations on input integers, but way before I can get there, it has a problem -- the scanf("%c"... deal seems to automatically get bypassed and the program quits (press any key). What's strange is that if I switch the order of the scanf's, s.t. the scanf("%c"... comes before the %d scanf's, everything is fine. What am I missing here?

int num1,num2;
    char o;
    float rslt;    
    printf("Enter 1st integer: ");
    printf("Enter 2nd integer: ");
    printf("Choose operation <a,s,m,d>: ");
    scanf("%c", &o);

Recommended Answers

All 2 Replies

Since all you are reading is a character, use getchar() . But remember that you did hit the ENTER key too. You therefore have to clear the buffer. So when you read a character, use something like

whatever = getchar();  // at least one character left in the input stream
    dummy = getchar();    // read another character
} while (dummy != '\n');  // stop when newline is read
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.