Do-While Loop - Sum of numbers

Your program should ask the user for a sequence of numbers until the user inputs 0 (zero) and then the program should print the sum of positive numbers that the user input.


I am not allowed to use functions, arrays, or constructs that I haven't studied. This will probably be limited to a "Do-While" and "If" statement.

I believe I am close, but I am confused about what operators to use to create a sum. The user may input as many numbers as he/she wants - so it is not a defined set of numbers.

Is my boolean expression correct?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Finds the Sum of Numbers
// Brian Kircher
// 01/30/2013

#include <iostream>

using namespace std;

int main(void){

  int sum=0; 
  int numbers;

  do{
    cout << "Input number [0 to stop program]: ";
    cin  >> numbers;

    if ( numbers > 0) ;
  }while(numbers != 0);

  cout << "The maximum number is: " << sum << endl;
}
You just need
 
    sum += numbers;

somewhere after line 16.

Strictly speaking, you should make this dependent on the "if" statement, but since adding zero to the total doesn't change the result, you could even miss that out.

The do-while looks reasonably ok.

The text on line 31 says "maximum number" which doesn't agree with your description of the problem.
Last edited on
Topic archived. No new replies allowed.