Click here to Skip to main content
15,916,941 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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
Comments
Rick York 3-Sep-18 19:23pm    
If you want five numbers entered then why require a zero to stop?

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.
 
Share this answer
 
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.
 
Share this answer
 
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
 
Share this answer
 
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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900