1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C++ Error Checking (Simple program)

  1. Feb 13, 2016 #1
    1. The problem statement, all variables and given/known data
    I am writing a program that will calculate the average and standard deviation given data by the user. The data is entered into a vector. The size of the vector is also determined by the user.

    When the user enters the size of their vector, they need to enter a positive integer, if they don't the program will tell them to do so (If they enter the letter A, the program will prompt them to enter a positive integer). This part I have completed.

    However, when they actually enter their data into the vector, if they enter the letter A or any other character besides a number, the program terminates and returns 0 for everything. How can I make it so that if they give the vector "bad data", my program can tell them it is not a number and have them re enter it.

    I bolded the part of the code that gets the values from the user.

    I am a complete novice when it comes to programming and am trying to self teach.

    3. The attempt at a solution
    Code (C):

    #include <iostream>
    #include <vector>
    #include <cmath>
    #include <exception>
    #include <typeinfo>
    #include <limits>
    using namespace std;

    float square(float x)    //Returns square of a number
        return x*x;

    float average(vector<float>& values)  //Returns average values
        float sum = 0;

        for(int i = 0;i<values.size();++i)
            sum+=values[i]; // Calculates the sum
        return sum/values.size(); // Calculates the average

    float stddeviation(vector<float>& values)      //Returns standard deviation
        float std_sum = 0;
        float mean = average(values);

        for(int i = 0;i<values.size();++i)
                std_sum += square(values[i]-mean);

        return sqrt(std_sum/(values.size()-1));

    // Main

    int main(int argc, const char * argv[])

        cout<< "Please enter the number of values: \n";

        int num;

        bool valid = false;

        while ( !(cin >> num) || cin.peek() != '\n' || num < 0)
            cout << "Enter a positive integer" << endl;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');

        vector<float> values(num);//Intialize values to hold num values
        cout<<"Please enter values, numbers only: \n";
        for(int i = 0; i<num;++i)  // Input values to vector

        cout<<"Average: "<<average(values)<<"\n";
        cout<<"Standard Devation: "<<stddeviation(values)<<"\n";
        return 0;   //End

    This is the part of the code where I want the user to be prompted if they enter "bad data" into the vector

    for(int i = 0; i<num;++i) // Input values to vector
    Last edited: Feb 13, 2016
  2. jcsd
  3. Feb 13, 2016 #2


    Staff: Mentor

    I edited your listing using the [_code=c_] tags but I lost your bolded code.

    Could you repost that section?
  4. Feb 13, 2016 #3


    Staff: Mentor

    I would adjust your for loop with an inner while loop where you input the data as a string via getline() and then do some checks and parse the string as a number.

    In this way cin won't get in the way reject it prematurely.
  5. Feb 14, 2016 #4
    Hmm, how would I do this? Should I make a string variable and getline(cin,string variable) ? I am not as familiar with getline, so if what I said makes no sense then I apologize :)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted