Getting input from user with Switch C++

此生再无相见时 提交于 2019-12-23 06:31:08

问题


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?


回答1:


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.




回答2:


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;



回答3:


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.



来源:https://stackoverflow.com/questions/32789578/getting-input-from-user-with-switch-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!