Switch语句读取C ++中的每个输入

时间:2018-10-10 22:34:29

标签: c++ switch-statement

我有这个switch语句,用于允许用户选择一个选项。用户输入9时退出。但是问题是,当我输入“ ah2”之类的内容时,它将显示为“ a”,然后显示错误消息,然后显示“ h”和错误消息,然后显示2。当用户输入此类输入时应停止操作,并应要求输入其他输入。

这是我的代码。

void menu()
{

    int choice = 0;

    cout<<"\nWelcome to the program menu."<<endl;
    cout<<"Choose the operation you would like to execute."<<endl;
    cout<<"1. Create "<<endl;
    cout<<"2. Look items "<<endl;
    cout<<"3. See "<<endl;
    cout<<"4. Get "<<endl;
    cout<<"5. See the value "<<endl;
    cout<<"6. Insert an item"<<endl;
    cout<<"7. Delete an item"<<endl;
    cout<<"8. Destroy "<<endl;
    cout<<"9. Press -1 to exit the program"<<endl;

    cout<<"Enter your choice: "<<endl;
    cin>>choice;

   while(choice != 9)
    {
        switch (choice)
        {
case 1:
            cout<<"You choose one"<<endl;
            break;
        case 2:
            cout<<"You choose two"<<endl;
            break;
        case 3:
            cout<<"You choose three"<<endl;
            break;
        case 4:
            cout<<"You choose four"<<endl;
            break;
        case 5:
            cout<<"You choose five"<<endl;
            break;
        case 6:
            cout<<"You choose six"<<endl;
            break;
        case 7:
            cout<<"You choose seven"<<endl;
            break;
        case 8:
            cout<<"You choose height"<<endl;
            break;
        case 9:
            cout<<"You choose nine"<<endl;
            break;
        default:
            cout<<"You have not entered a number proposed. Retry. "<<endl;
            choice = 0;
            break;
        }
        cin.clear();
        cin.ignore();
        cout<<"Enter your choice : "<<endl;
        cin>>choice;
    }
    cout<<"\nYou have entered the exist option end the program"<<endl;
}

1 个答案:

答案 0 :(得分:0)

""更改为x = matrix(rnorm(1000*100),ncol=100) microbenchmark::microbenchmark(.colSums(x,1000,100), colSums(x),Rfast::colsums(x,parallel=0),times=1000) Unit: microseconds expr min lq mean median uq max neval .colSums(x, 1000, 100) 124.870 125.725 127.28580 125.726 126.153 301.911 1000 colSums(x) 132.567 133.423 136.30507 134.705 135.134 282.668 1000 Rfast::colsums(x, parallel = 0) 79.541 80.824 84.00742 81.252 82.107 307.470 1000