Average numbers loop

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

int main()
{
  int num;
  double input, sum=0, avg;

  cout << "Enter a number for how many scores you'd like to put. \n\n"
	  << "After you enter a number for the amount of scores, \n"
	  << "proceed by entering any number you please until you have \n"
	  << "reached the limit of the number you placed before.\n\n"
	  << "Or if you wish to terminate the program early, please enter #. \n\n";
  cin >> num; 
  
    for(int i = 1; i <= num; i++)
	{
      cin >> input;
      sum += input;
    }

  avg = sum / num;
  cout << "Average score of the numbers = " << avg << endl;

return 0;
}


For this program, I have to make a list number for the amount of scores the user wants to input and then have it come out. I have a decent loop to put the numbers in. The issue is that I can't set the program to terminate when I use the # symbol say the user wants to terminate the program early, I tried a for...while and it didn't work. Whenever I did use it, the list itself wouldn't work period, it wouldn't calculate the average. Any advice or aid? Thanks everyone!
Last edited on
in line 25,

avg = sum / num;
assuming the # symbol work for stopping the user input, the code above wouldn't calculate the correct average...

You can declare a new variable to count how many user input and divide the sum with that count variable,

and you can eliminate the "for-loop" and cin >> num;
So user can input as many number as they want without telling the program first.

as for the symbol, i thought the 'num' variable which is integer can't hold '#' cmiiw,
you can change the '#' to integer like '-1', then change the while loop to

input = 0;
count = 0;
while(input!=-1){

cin >> input;

if(input==-1){
}else{
sum += input;
count ++
}
}

Last edited on
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  int num;
  double input, sum=0, avg;
  double count;
  bool again = true;
  char yn = 'Y';
  cout << "Enter a number for how many scores you'd like to put. \n\n"
	  << "After you enter a number for the amount of scores, \n"
	  << "proceed by entering any number you please until you have \n"
	  << "reached the limit of the number you placed before.\n\n"
	  << "Or if you wish to terminate the program early, please enter #. \n\n";
  cin >> num; 
  

input = 0;

count = 0;

sum += input;

while(input != -1){
	cin >> input;

	if(input == -1){
	}
	else if (sum += input, count ++)
	{
	}
}
  avg = sum / num;
  cout << "Average score of the numbers = " << avg << endl;

  {
  cout << endl << "Would you like to enter another set of numbers and retrieve a new average?" << endl;
  cin >> yn;
  cout << endl << endl;
  
  if (yn == 'n' || yn == 'N')
	again = false;
  else again = true;
  }
return 0;
}


After doing what I thought was right, after inputting the ideas you told me it worked great. I've been attempting to gut other programs that I have and try inputting some of their stuff so it can prompt the user to input Y or N if they want to add another set of numbers. It ends the program regardless though.

EDIT: Nevermind, found what I did wrong. Turns out doing an algorithm first does help and makes you notice that you require a while loop in the beginning for the user to choose if they want to do it again. THANKS A BUNCH GENTS!
Last edited on
Topic archived. No new replies allowed.