C++ Loop Outputting Average From Set of Numbers With Decimals

******See code update in one of the posts below. Any help greatly appreciated!*******

Hello,

Beginner C++ student here, first programming class. I am trying to build a program which will accept a set of numbers from the user and output the average. However, when attempting to get the average of example 1 I get 3.5. Example two I get -0. Any help is greatly appreciated!

Examples:

Output the average of the values entered.

Example 1: A run of your program might go as

1 2 3 4 blah

2.5
Example 2: A run of your program might go as

blah

no data

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

  #include <iostream>

using namespace std;

int main(){


  double num;
  double input;
  double sum = 0;
  double avg;

  cout << "Please enter a set of numbers. Enter a non-number to quit: "
	  << endl;
  cin >> num;

  while (cin >> num) // When value is not a number, quit.

    for(int i = 1; i <= num; i++){

      cin >> input;
      sum += input;
    }

  avg = sum / num;
  cout << "average = " << avg << endl;

return 0;

}
Last edited on
What's with that for loop? Why not just scrap that and add num to sum right away? Also, you need to track how many inputs you receive.
Thanks for the reply! The professor would like us to use a loop for this program, as we are learning loops right now. :0(
You already have a while loop - are you specifically required to use a for loop? It isn't a good fit for this program.
Ah, I see what you mean. I worked with the while loop and came up with the code below. However, now I get

1
Enter a number: 1
2
Enter a number: 2
3
Enter a number: 3
4
Enter a number: 4
blah

The Average is: 2.5
Press any key to continue...

I am unsure as to why I have to enter a value prior to the program returning "Enter a number: ". However, at least I am now getting the correct average. Any help with that? Thank you so much!

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

#include <iostream>

using namespace std;

const int numToLimit = 4;

int main(){


	double sum = 0;
	double average = 0;
	double input;

	// Get 10 numbers from the user

	//for (int i = 0; cin >> numToLimit; ++i)

	while (cin >> input) 

	{
		cout << "Enter a number: ";
		cin >> input;

		int i = 1;
		i <= numToLimit;

		sum += input;

	}

	// Get average and print it
	average = sum / numToLimit;

	cout << "The average is: " << average << endl;

	return 0;
}
Last edited on
Move the output to the loop condition before the input:
19
20
while((std::cout << "Enter a number: ") && (std::cin >> input))
{
Note that this sort of thing only works for stream classes because of their ability to be implicitly converted to bool.

See also:
http://www.LB-Stuff.com/user-input
Last edited on
Thank you so much for you help. Working 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include <iostream>
#include <string>
#include <cstdlib> // exit EXIT_FAILURE

using namespace std;

bool die(const string & msg);

int main(){

	int numToEnter;
	double sum = 0;
	double average = 0;
	double input;

	// Get numbers from the user

	cout << "Enter number of items: ";
	cin >> numToEnter || die("Input failure, only numbers allowed.");

	while ((cout << "Enter a number: ") && (cin >> input))

	{

		int i = 1;
		i <= numToEnter;

		sum += input;

	}

	// Get average and print it
	average = sum / numToEnter;

	cout << "The average is: " << average << endl;

	return 0;
}

bool die(const string & msg)

{
	cout << "Fatal Error: " << msg << endl;

	exit(EXIT_FAILURE);

}
Last edited on
Topic archived. No new replies allowed.