C++ Primer exercise

Hello. Can someone explain to me why the part with the total is not working properly?
I tried a different approach and it worked but still can't figure out what exactly goes wrong here

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
  /* Write a program that reads up to 10 donation values into an array of double. (Or, if
you prefer, use an array template object.) The program should terminate input on
non-numeric input. It should report the average of the numbers and also report
how many numbers in the array are larger than the average.
*/



#include <iostream>


using namespace std;

int main()
{	double total;
	int bigger=0;
        int values=0;
	double donation[10];
	
	cout<<"Enter 10 donation values(numbers only). ";
	cout<<"Enter first value: ";

	while (values<10 && cin>>donation[values] )
	{	++values;
		if (values<10)
		{
		cout<<"Enter value no " <<(values+1)<<" :";
		total+=donation[values];
	}
	}
	double average=total/10;
	
	for ( values=0; values<10; values++)
	{
	if (donation[values]> average)
	{
		bigger++;
	}
	
    }
	cout<<"The average of the numbers is: "<<average<<endl;
	cout<< bigger <<" numbers larger than average. \n";
	cout<<"Bye";
	
	
	
	
	return 0;
}

		

Hello georgy90,

Looking at the while loop,which BTW would read like this:
1
2
3
4
5
6
7
8
9
while (values<10 && cin >> donation[values])
{
	++values;
	if (values<10)
	{
		cout << "Enter value no " << (values + 1) << " :";
		total += donation[values];
	}
}


You start with "value" initialized to zero then on line 3 you start by adding one to "value" which has just shortened your array to 9 and when you try to enter the last number you have gone past the end of your array which causes a run time error.

Your loop should work better this way:
1
2
3
4
5
6
7
8
9
while (values < 10 && cin >> donation[values])
{
	//++values;
	if (values < 10)
	{
		cout << "Enter value no " << (values + 1) << " :";
		total += donation[values++];  // <--- Add here when you are done using "value".
	}
}


Tip: lines 33 - 40 when you have a single for loops and if/else statements the {}s are OK, but not needed.

This will work:
1
2
3
for (values = 0; values < 10; values++)
	if (donation[values]> average)
		bigger++;


Hope that helps,

Andy
Hello Andy,

thanks for the elaborate answer and the tips!
Still not sure I got the mistake at the part with the while loop though: I thought that the first time the while loop tests it's parameters, it reads cin to donation[0] because values is zero at the time..then I increment it so that the next time it gets to the while loop and tests, the cin will read the input in donation[1]. So by the time the while loop ends, tha array positions [0] to [9] have filled and the array is ok..?
So the problem is with the total, which starts with adding from donation[1] because I incremented the values variable before the loop gets to total the first time? But shouldn't the if statement be executed 9 times anyway, and not 10? I mean that the mistake I was expecting was that total would hold the total value of donation[1]up till donation [9] because by the time the donation[9] gets the input during the while loop test, then the 9 is incremented to 10 so the if(values<10) does not executed at all..?
I am sure the case is simple, I just still don't fully understand it.
Hello georgy90,

Look at the code in this way:
1
2
3
4
5
6
7
8
while (values < 10 && cin >> donation[values])
{
		total += donation[values];  // <--- Add here when you are done using "value".

		cout << "Enter value no " << (values + 1) << " :";

		values++;
}

"values" is zero you reach the while loop and the condition is checked. Since the lhs is true it does the "cin" you enter a number and if the rhs is true it enters the loop. The first line executed is to add "donations[0]" to "total". At this point in your code you are adding "donations[1]" to total and missing "donations[0]: because you increased "values" to soon. The "cout statement is using "values" which is still zero, so it prints the correct number. The last statement now adds 1 to "values" just before you go back to the while condition.

You will notice the if statement is missing. That is because this check has already been done in the while condition.

After a little sleep I can better work on the program when I load it up and try it.

Hope that helps,

Andy
Hello georgy90,

After working with the program I did find the if statement inside the while loop is useful, but it was being used wrong.

I figured it out to work this way:
1
2
3
4
5
6
7
while (values < 10 && cin >> donation[values])
{
	total += donation[values++];
		
	if (values < 10)
		cout << "Enter value no " << (values + 1) << " : ";
}

Before I changed the if statement it was prompting for number 11, but did not wait for input. In a way it worked correctly, it just asked for more than needed.

I believe this should work for you.

Hope that helps,

Andy
Great. Let me know if you discover something more about the issue! Thanks for the declarations and the functional alternatives you have proposed
Topic archived. No new replies allowed.