Search code examples
c++vectorwhile-looptry-catchexcept

using exception to store user input as either double or string


Okay, I am very new to try-except. I do-not know if what i'm trying to do is possible, but thought i'd ask for input anyway.

My program is supposed to average x amount of user inputs until he/she enters q.

Here is the function that is giving me the most problems.

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;

}

I need some insight on why this program won't compile. Any help would be appreciated


Solution

  • You haven't clearly specified your requirements which makes it difficult to know how to answer the question. I'm assuming you want something like this:

    #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;
            }
        }
    }
    

    In future, make sure all the code you post in future questions can be compiled and ran by anyone, especially for simple stuff like this. You can use http://ideone.com/ to test your code samples before posting.