Print items

closed account (yboiAqkS)
made a program that displays the sum, the minimum value entered, the maximum value entered. But Don't know how to print out the number of items entered.
#include <iostream>

using namespace std;

int main()
{
double sum = 0, mean = 0, min = 0, max = 0;
while(true){
double num;
cout << "Enter a number(0 to exit): ";
cin >> num;
if(num == 0){
break;
}
sum += num;
mean++;
if(min > num){
min = num;
}
if(max < num){
max = num;

}

}
double avg = sum /mean;
cout << "Sum of all num = " << sum << endl;
cout << "Minimm of all numbers = " << min << endl;
cout << "Max of all numbers = " << max << endl;
cout << "Average of all numbers = " << avg << endl;
}
Last edited on
Not sure I understood correctly; nevertheless, here it goes to give you an idea.

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
#include <iostream>
using namespace std;

int main()
{
	double sum = 0, mean = 0, min = 0, max = 0;
	int countItemsEntered = 0; // counter

	while (true) {
		double num;
		cout << "Enter a number(0 to exit): ";
		cin >> num;
		countItemsEntered++; // count each number inputted.
		if (num == 0) {
			break;
		}
		sum += num;
		mean++;
		if (min > num) {
			min = num;
		}
		if (max < num) {
			max = num;

		}

	}
	double avg = sum / mean;
	cout << "Sum of all num = " << sum << endl;
	cout << "Minimm of all numbers = " << min << endl;
	cout << "Max of all numbers = " << max << endl;
	cout << "Average of all numbers = " << avg << endl;
	cout << "Total number of items entered = " << countItemsEntered << endl; // Print it out the total counter.
}
Topic archived. No new replies allowed.