使用exception将用户输入存储为double或string

时间:2014-10-11 01:19:12

标签: c++ vector while-loop try-catch except

好的,我很想尝试 - 除外。我不知道我试图做的事情是否可行,但我还是想请求输入。

我的程序应该平均x个用户输入量,直到他/她进入q。

这是给我最大问题的功能。

vector user_input() {

    vector<double> scores;
    string user_command;
    do{
        double user_input;
        cout << "Enter the scores to be averaged (range 0-100) or enter q to quit: " << endl;
        cin >> user_command;
        if (is_number(user_command))
        {
            user_input = atof(user_command.c_str());
            if (user_input < 0 || user_input > 100)
                cout << "This is not within range!" << endl;
            else{
                scores.push_back(user_input);}
        }

    }
    while (user_command != "q" && user_command != "Q");
    return scores;

}

我需要深入了解这个程序为什么不能编译。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您还没有明确说明您的要求,因此很难知道如何回答这个问题。我假设你想要这样的东西:

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

typedef double score_type;  //so we don't need to write 'double' everywhere, makes it easy to change the type

void user_input(std::vector<score_type>& scores)
{
    std::string command;

    for (;;)
    {
        score_type score;

        std::cout << "Enter the scores to be averaged (range 0-100) or enter q to quit: " << std::endl;

        std::cin >> command;

        if (command == "q" || command == "Q")
        {
            //works better than a do-while in this case
            break;
        }

        try
        {
            //stod to throw std::invalid_argument and std::out_of_range, check documentation (http://en.cppreference.com/w/cpp/string/basic_string/stof)
            score = std::stod(command.c_str());
        }
        catch (std::exception e)
        {
            //build exception string
            std::ostringstream oss;
            oss << "bad input: " << command;

            //throw new exception to be handled elsewhere
            throw std::exception(oss.str().c_str());
        }

        if (score < 0 || score > 100)
        {
            std::cerr << "Input  is not within range (0-100)!" << std::endl;
        }

        scores.push_back(score);
    }
}

int main()
{
    for (;;)
    {
        std::vector<score_type> scores;

        try
        {
            user_input(scores);
        }
        catch (std::exception e)
        {
            std::cout << "input exception: " << e.what() << std::endl;
        }

        score_type score_sum = 0;

        for (auto score : scores)
        {
            score_sum += score;
        }

        score_type average_score = 0;

        if (!scores.empty())
        {
            average_score = score_sum / scores.size();
        }

        std::cout << "average score: " << average_score << std::endl;
        std::cout << "go again? (y/n): " << std::endl;

        std::string command;

        std::cin >> command;

        if (command == "y")
        {
            continue;
        }
        else if (command == "n")
        {
            break;
        }
    }
}

将来,请确保您在以后的问题中发布的所有代码都可以由任何人编译和运行,特别是对于这样的简单内容。您可以在发布之前使用http://ideone.com/来测试代码示例。