How to make an error statement

I wrote the code to ask the user to input 5 numbers. They are then passed to a new function named sum where they are added together and then sent back to the main. In the main I then find the average.

I want to write something that says "error" if the user input anything besides a number during the input of data at the start.

#include <iostream>


using namespace std;
float sum(float a1, float b1, float c1, float d1, float e1);

int main()
{
//input data
float a, b, c, d, e, ans, final;
cout<<"Enter the first value""\n";
cin>>a;
cout<<"Enter the second value""\n";
cin>>b;
cout<<"Enter the third value""\n";
cin>>c;
cout<<"Enter the forth value""\n";
cin>>d;
cout<<"Enter the fifth value""\n";
cin>>e;
ans=sum(a, b, c, d, e);// sent to z_function
final=ans/5;
cout<<"The answer is "<<final<<"\n";


}
float sum(float a1, float b1, float c1, float d1, float e1)

{

//math
float x;
x=a1+b1+c1+d1+e1;

return (x); //send back to main
}
When you post please use codetags
http://www.cplusplus.com/articles/z13hAqkS/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <functional>
using namespace std;

int main()
{
    double number = 0;

    cout << "Plese enter a number: " << flush;
    while(!(cin >> number))
    {
        cin.clear();
        cin.ignore(10000, '\n');
        cout << "Error Thats not a number" << endl;
        cout << "Plese enter a number: " << flush;
    }
    cin.ignore(1000, '\n');

    cout << "Your number was " << number << endl;

    return 0;
}
Topic archived. No new replies allowed.