简体   繁体   English

如何为开关盒提供可靠的用户输入?

[英]How to make a robust user input for a switch case?

I am trying to make a completely error-proof input for switch cases. 我正在尝试为开关盒提供完全防错的输入。 It needs to not fail if the user puts in the wrong number, a letter, or a long string of numbers or letters (This is where I have had errors before). 如果用户输入了错误的数字,字母或一长串数字或字母(这是我之前遇到过错误的地方),它就不会失败。

To prevent an error if the user inputs eg. 为了防止错误,如果用户输入例如 "asdghdk3" I have tried using an array so it will check each letter until it finds a number. 我尝试使用"asdghdk3"数组,因此它将检查每个字母,直到找到一个数字。

I have then tried to turn it into an integer for the switch case. 然后,我尝试将其转换为开关情况的整数。 Sadly my code will not work. 可惜我的代码无法正常工作。 Does anyone have any suggestions or improvements? 有没有人有任何建议或改进? Thank you. 谢谢。

#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
    cout<<"Please choose the method you would like to use to evaluate the potential. Please enter 1,2 or 3:"<<endl;
    cout<<"1. my method. \n2. jacobi method. \n3. Exit programme. \nYou chose: ";

    char choice[20];
    bool k = true;

    int choice2;
    while (k == true){
        fgets(choice, sizeof choice, stdin);
        for(int j=0; j<sizeof(choice); j++){
            if (isdigit(choice[j])==true){  //I want it to check every character until it finds a number.
                choice2 = atoi(choice[j]); //changed name as now integer to be used in switch case.
                k = false;
                break; //so that it breaks out of the for loops because it has found a number
            }
            else{
                continue;
            }
        }
        cout<<"Incorrect input please try again";
    }

    cout<<"\nchoice2= "<<choice2<<endl;

    switch ( choice2 ) {
        case 1 :
            // Code
            break;
        case 2:
            // Code
            break;
        case 3:
            //code to exit programme
            break;
        default:
            // Code
            break;
    }

    return 0;
}

EDIT: I would like it to only accept 1, 2 or 3 and for everything else return incorrect input please try again. 编辑:我希望它只接受1、2或3,对于其他所有返回错误输入的内容,请重试。

using namespace std; 使用名称空间std;

int main()
{
    string line;
    getline(cin, line);
    istringstream choice(line);
    int number;
    choice >> number;

    if (choice)
    {
        if (choice == 1)
        {
            cout << "you chose option 1\n";
            //code.....
        }
        else if (choice == 2)
        {
            cout<< "you chose option 2\n";
            //code.....
        }
        else if (choice == 3)
        {
            cout<< "you chose option 3\n";
            //code......
        }
    }
    else
    {
        cout << "input does not start with a number or is too big for an int\n";
    }
return 0;
}

You should be using std::cin and checking its status: 您应该使用std::cin并检查其状态:

int choice = -1;
if (cin >> choice)
{
    // you know user entered a number, check that it's in the correct range

    if (cin.peek() != '\n')
        // there's more input, so probably an error
}
else
{
    // bad input
}

Read a whole line from std::cin into a std::string with std::getline , then convert the line to an integer number with a std::istringstream . std::cin的整行读取为带有std::getlinestd::string ,然后使用std::istringstream将行转换为整数。 Finally, after the conversion, check if there are characters left in the string stream. 最后,在转换之后,检查字符串流中是否还有字符。 Here's a complete example: 这是一个完整的示例:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string line;
    std::getline(std::cin, line);
    std::istringstream is(line);
    int number;
    is >> number;

    if (is)
    {
        if (!is.eof())
        {
            std::cerr << "input does not end with a number\n";
        }
        else
        {
            std::cout << "input ok\n";
        }
    }
    else
    {
        std::cerr << "inut does not start with a number or is too big for an int\n";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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