15,747,189 members
See more:
Hei! I'm programming in C right now with CLion, and I'm new to this.

the question is:

"Write a program that asks the user for numbers until 0 is given, then:

Prints out how many numbers have been given (excluding the 0):
Count: 5
Prints out the sum of all the numbers:
Sum: 78
Prints out the average:
Average: 15.6"

but I can't seem to print the sum? and the average I don't know how to.

What I have tried:

C++
```int main()
{
int input = 5;

while(input != 0)
{
printf("Enter 5 number (0 to stop): ");
scanf("%i", &input);
}

int sum = input;
printf("The sum is: %i", sum);

return 0;
}```
Posted
Updated 7-Nov-22 22:36pm
v2
Rick York 3-Sep-18 19:23pm
If you want five numbers entered then why require a zero to stop?

## Solution 1

Sum and average are simple mathematical calculations. Write down the operations necessary to do it, and you can then convert what you have written into code.

## Solution 2

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

## Solution 3

Learn to use the debugger as Patrice wrote and you find your mistakes and missing code.

Tips:
1. count the numbers of input in the loop
2. you must sum up in the loop (because the value got overriden with the next input)
3. output of numbers of input after the loop

## Solution 5

Your code should look like this

C++
```int main()
{
int input = 5;
int sum = 0;
int count = 0;

while(input != 0)
{
printf("Enter 5 number (0 to stop): ");
scanf("%i", &input);
sum += input;
if (input != 0) ++count;
}

//int sum = input;
float avg = (float) sum;

if (count != 0) avg /= count;

printf("Count is: %d\n", count);
printf("The sum is: %d\n", sum);
printf("Average is: %.2f\n", avg);

return 0;
}```