I'm trying to output a specifi error.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 #include <iostream>
#include <cstdlib>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;

double sort(double input[], int N)
{
	double temp;
	int i, j;
	
	for (i=0; i<=8; i++)
	{
		for (j=i+1; j<=9; j++)
		{	
			if(input[i]>input[j])
			{
				temp=input[i];
				input[i]=input[j];
				input[j]=temp;
	        }
		}
	}
	return input[N];
}

int main()
{
	const int N=10;
	double x, y, sum, total, input[N];
	int i;
	sum=0;
	x=1;
   
	cout << "Enter 10 numbers: " << endl;
	for (i=0; i<=9; i++)
	{
		if (x<=10)
		{
			cout << "Number " << x << ": "; 
			cin >> input[i];
			x++;
		}
		else if (x==10)
		{
			break;
		}
	}

	sort(input, N);
	
	cout << "Here are the numbers in sorted order:" << endl;
	
	for (i=0; i<=9; i++)
	{
		cout << input[i] << " ";		
	}
	
	y=((input[N/2] + input[N/2-1])/2);
	cout <<"\nThe median is " << y << endl;
	
	for(i=0; i<=9; i++)
	{	
		sum=sum+input[i];
	}
	
	total=(sum/10);
	cout << "The mean is " << total << endl;
	
	return EXIT_SUCCESS;
}


I'm trying to give an error message when the user doesn't enter a number, but I haven't been able to get it to work.

this is what I tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	cout << "Enter 10 numbers: " << endl;
	for (i=0; i<=9; i++)
	{
		if (x<=10)
		{
			cout << "Number " << x << ": "; 
			cin >> input[i];
			if (!cin)
                        {
                              cin.clear();
                              cout << "Error: You Must Enter A Number. " << endl;
                              cin >> input[i];
                         }
                        x++;
                        
		}
		else if (x==10)
		{
			break;
		}
	}

I tried that out but it would repeat the error for all 10 input, instead of giving an error for that 1st one. I want to give an error for that one time they enter a letter, then have the program tell them to redo their input, then have it proceed to the next number "number 2: " So anyone have any ideas of what I should try?
Try this, we need to read and ignore the incorrect input

while (!cin)
{
char str[10];
cin.clear();
cout << "Error: You Must Enter A Number. " << endl;
cin >> str; // Ignore the incorrect input
cin >> input[i];
}
worked perfectly. Thanks
Topic archived. No new replies allowed.