Average of 3 numbers C++

Hello everyone I'm a beginner to coding and I would like some help with something I have encountered while trying to write some code. This is not homework just something I am doing for fun to try to advance my coding skills a little bit more. I am building code that will take 3 numbers that the user inputs then finds the average of those 3 numbers. I have that part down but I'm trying to think of some ways I can go about figuring out error handling situations such as if someone puts a letter instead of a number I would like it to cout "Error must type in a number". But I'm not sure what code I would use exactly.. I know it should need an if statement that states if int does not equal a number, then cout error message but I'm lost when it comes to the syntax. Any ideas as to how I can go about this? Below is what I have so far.

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
26
27
28
 #include <iostream>

using namespace std;
int main()
{
	int a;
	int b;
	int c;

	int average; 

	cout << "Enter a number: " << endl;
	cin >> a;

	cout << "Enter another number: " << endl;
	cin >> b;

	cout << "Enter your last number: " << endl;
	cin >> c;

	average = (a + b + c) / 3;
	cout << "Average is:\n" << average << endl;


	 
	system("pause");
	return 0;
}
Last edited on
Read this and next items (menu is on the left side of the page)
http://www.parashift.com/c++-faq/stream-input-failure.html
Thank you, all help is appreciated!
Topic archived. No new replies allowed.