【问题标题】:Getting numbers from user input until user types "quit"从用户输入中获取数字,直到用户键入“退出”
【发布时间】:2016-05-28 10:32:16
【问题描述】:

我正在开发一个程序,允许用户输入任意数量的双打,并将这些双打添加到向量中,直到用户输入“退出”,然后退出循环。

我需要这个输入函数在用户输入字符串或字符时不会失败,所以while (cin >> x) 是不可能的。

这是我的代码:

vector<double> input()
{
    double x;
    vector<double> scores;
    cout << "Please enter a score: ";
    while(true)
    {
        x = checkInput();
        scores.push_back(x);
        cout << "Enter another: ";
    }
return scores;
}

double checkInput()
{
    double x;
    cin >> x;
    while(cin.fail())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "That is not a number. Please enter another: ";
        cin >> x;
    }
    return x;
}

这有效,并且如果用户输入无效输入也不会中断。但是,正如您所见,它永远不会从输入循环中中断。当用户输入==“退出”时,我需要它来打破并返回分数。我怎样才能做到这一点?

【问题讨论】:

  • 使用getline() 阅读第一名。然后用std::istringstream提取你需要知道的内容。
  • 使用 getline() 读取 std::string 然后使用 stod() 提取双精度值。如果它在读取字符串时需要继续工作,那么您需要使用字符串。
  • @JacobH 我试过这个方法,但是如果用户输入的不是“quit”(会中断)或数字,那么 stod() 会使程序崩溃。
  • @Kenta : 在使用 stod() 之前测试输入是否有效。

标签: c++ validation input while-loop cin


【解决方案1】:

使用 std::string 并转换为 int

这是一个sn-p

double checkInput(bool& dontQuit)
{
    string sx;
    double x;
    while(dontQuit)
    {
        cin >> sx;
        dontQuit = sx != "quit";
        try{
            x = stod(sx);
            break;
        }
        catch(std::exception& e){
            cout << "please Enter a number!: ";
        }
    }
    return x;
}

在这种情况下,您的 input() 函数将会改变...

bool dontQuit = true;
while(dontQuit)
{
    x = checkInput(dontQuit);
    scores.push_back(x);
    cout << "Enter another: ";
}

【讨论】:

    【解决方案2】:

    我会这样做:

    #include <iostream>
    #include <vector>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    bool isOnlyDouble(const char* str) {
        char* endptr = 0;
        strtod(str, &endptr);
    
        if(*endptr != '\0' || endptr == str)
            return false;
        return true;
    }
    
    int main() {
            vector<double> v;
            double d;
            string input;
            while(1) {
                    getline(cin, input);
                    if(input == "quit")
                            break;
                    // else it should be a number
                    if(isOnlyDouble(input.c_str())) {
                            d = atof(input.c_str());
                            v.push_back(d);
                    } else {
                            cout << "found no number in the string\n";
                    }
            }
            for(unsigned int i = 0; i < v.size(); ++i)
                    cout << v[i] << endl;
            return 0;
    }
    

    输出: gsamaras@gsamaras:~/Desktop$ g++ -Wall -std=c++0x px.cpp gsamaras@gsamaras:~/Desktop$ ./a.out

    3.14
    foo
    found no number in the string
    foo2.56
    found no number in the string
    2.78
    quit
    3.14
    2.78
    

    想法:

    1. 我们正在使用std::getline() 循环获取输入。
    2. 如果输入为“退出”,则中断循环。
    3. 其他:
      1. 如果它只包含一个双精度值,则推入向量。
      2. 否则,通知用户。

    我决定不从 w2.56 中提取值,因为它可能是一个错字。


    推荐这个answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 2016-01-19
      相关资源
      最近更新 更多