program skips through all of the array inputs

Writing a program that reads up to 10 donation values into an array of double and must terminate input on non-numeric input. It should also report the average of the numbers.

The problem is it only reads 1 input and then shows me the average of zero.
Plus if I type large numbers like 50 or higher it crashes. But if I type in say 10 it just proceed to the average calculations and skips all of the other 9 array inputs to be performed.

The code:

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>
using namespace std;
const int Size = 10;

int main()
{
	double * donation = new double[Size];
	double sum = 0;
	for (int i = 0; i < Size; i++)
	{
		cin >> donation[i];
		if (!double(isdigit(donation[i])))
			break;
		else
		{
			sum += donation[i];
			continue;
		}
	}
	cout << "The average is: " << sum/Size << endl;

	delete [] donation;
	system("pause");
	return 0;
}
Last edited on
On line 12 you need (!cin && !(isdigit(donation[i])))
Worked, but if I type in a non-numeric input like say character "k" the program crashes:

Debug Assertion Failed!

Program:...
File: f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c
Line: 56

Expression: (unsigned)(c + 1) <= 256

...


Could it be because of the type-cast from int to double on Line 12?
What do you mean by program crashes? When I type in "k", the program does what it's supposed to do: end by outputting "The average is 0".
Strange I get the error mentioned above.
isdigit() takes a char and returns a bool...in this case it doesn't make sense to do it at all. Just check for !cin and you should be fine.
Got it.
Thanks.
Topic archived. No new replies allowed.