Click here to Skip to main content
14,087,162 members
Rate this:
 
Please Sign up or sign in to vote.
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:

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 3-Sep-18 22:10pm
v2
Comments
Rick York 3-Sep-18 19:23pm
   
If you want five numbers entered then why require a zero to stop?
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

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
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190518.1 | Last Updated 4 Sep 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100