While loop for adding numbers infinitely

Write a program that will read in a list of numbers and print the sum of the numbers and the average. (Note: your program must work no matter how many numbers are in the list. You need a while loop for this
So far this is what I have...

1
2
3
4
5
6
7
8
9
10
11
12
13

 #include <iostream>
  using namespace std;
  main ()
 {
   int num, sum, average;
    while(cin>>num)
    {
       
    }

 }
  


I am stuck. I dont know what to do next. Please I need help and Thanks in advance!!
first of all, using namespace std; is bad. i would suggest getting out of the habit of using it. second of all, you need to have a count of how many times the loop has gone. third, initialize sum (to 0). finally, you just add the value of num and sum and save it in sum, then divide by the loop count, and there is your average.\

edit: was pointed out to me that you are also missing a return type for main
Last edited on
One more thing: the average can be a real number, so define it as a double. Also, if you just divide the integer sum by the integer count, you'll get an integer, so to compute the average, you'll want to do something like average = static_cast<double>(sum) / count;
average = static_cast<double>(sum) / count;

why in heaven or hell would you ever think of doing that?!?? a static_cast is most certainly not needed. not when you can just make the average a double. static_cast-ing is far from neccesary and you dont really need it for char -> int -> float -> double.
a static_cast is most certainly not needed. not when you can just make the average a double.

Are you sure?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using std::cout;
int main()
{
    int sum = 17;
    int count = 5;
    double average = sum / count;
    cout << "without cast average = " << average << '\n';
    average = static_cast<double>(sum) / count;
    cout << "with cast average = " << average << '\n';
    return 0;
}

without cast average = 3
with cast average = 3.4

The problem is that line 7 computes sum/count (an integer) first, then it promotes the result to a double for the assignment to average. To get the right value you have to ensure that the arithmetic is done using doubles, hence the cast.

no matter how many numbers are in the list

Note: If that really goes to "very many", then there will be more than any basic integral type can count, and the sum might be even bigger. Here, however, I guess getting that loop to look right is enough.


Nevertheless. Can the count be negative? Obviously not. That logical fact can be expressed and enforced in the code by using an unsigned type.
The problem is that line 7 computes sum/count (an integer) first, then it promotes the result to a double for the assignment to average. To get the right value you have to ensure that the arithmetic is done using doubles, hence the cast.

ofc... thats pretty standard knowledge. why would you divide integers and expect otherwise? you would need the others to be double to. your static_cast is still pointless and shouldnt be used here
Please, explain the pointlessness.
Perhaps I'm missing something. When you wrote
a static_cast is most certainly not needed. not when you can just make the average a double
I thought you meant that the code could do this:
1
2
3
4
5
int sum=0;
unsigned count = 0; // using Keskiverto's point
double average;
...
average = sum / count;

My point is that the last line won't work. I solved it with a cast. How would you suggest doing it?

Topic archived. No new replies allowed.