简体   繁体   中英

Getting input from user with Switch C++

I have a system where user can enter as many inputs as s/he wants and make some calculations.

Here is the code for achieving this task:

int main() {
    char op = 's';
    float time=0, fuel_rate=0, start=0, end=0, pace=0;


    while(op != 'x'){
        cout << "Please select: " << endl;
        cout << "1 ---> A" << endl;
        cout << "2 ---> B" << endl;
        cout << "3 ---> Calculate" << endl;
        cout << "x ---> Exit" << endl;

        op = std::getchar();

        //remove the rest of the line from input stream
        int temp;
        while ( (temp = std::getchar()) != '\n' && temp != EOF );

        switch(op){
        case '1':
            cout << "Enter time: ";
            cin >> time;
            cout << "Enter fuel rate: ";
            cin >> fuel_rate;
            break;
        case '2':
            cout << "Enter start: ";
            cin >> start;
            cout << "Enter end: ";
            cin >> end;
            cout << "Enter pace: ";
            cin >> pace;
            cout << "Enter fuel rate: ";
            cin >> fuel_rate;
            break;
        case '3':
            cout << "Total value";
            break;
        case 'x':
            return 0;
        default:
            continue;
        }
    }
    return 0;
}

System works well for the first input. Sample console log looks like this:

 Please select: 
1 ---> A
2 ---> B
3 ---> Calculate
x ---> Exit
1
Enter time: 2
Enter fuel rate: 3
Please select: 
1 ---> A
2 ---> B
3 ---> Calculate
x ---> Exit
2
Please select: 
1 ---> A
2 ---> B
3 ---> Calculate
x ---> Exit

First user enters the operation 1, system asks for time and fuel rate. When user enters the operation 2, system doesn't ask for start, end or pace.

Any ideas on how to solve this?

I am fairly certain std::getchar() is the cause of most of your problems. If I change your code to use:

cin >> op;
switch (op) { 
//...

Instead of

op = std::getchar();

//remove the rest of the line from input stream
int temp;
while ( (temp = std::getchar()) != '\n' && temp != EOF );

switch(op){
//...

The program runs just fine.

You are mixing the use of std::cin and stdin . You should stick with one of the. Instead of

    op = std::getchar();

use

    op = cin.get();

You should move the lines:

    int temp;
    while ( (temp = std::getchar()) != '\n' && temp != EOF );

after the end of the switch block, which making sure that you use temp = cin.get()

Unconsumed newline characters are left in the input stream since you are using operator>>() to read data, like:

    cin >> fuel_rate;

Adding debugging print code to the default case of your switch shows clearly what is going on:

// ...
default:
  cout << "unexpected: " << int(op) << endl;
  continue;
// ...

unexpected: 10

The decimal 10 is the newline \\n that is still in the input buffer after you did formatted input using operator>> on std::cin .

To correct this you could ignore leftover characters (think of what happens when the user doesn't enter a number when you requested one) from the stream up to and including the next newline character, after you did the formatted input:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Or use formatted input (which does the skipping on its own):

cin >> op;

Though you also need to take care of end of file conditions, which your current code fails to do as you see in the example run above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM