So i'm having troubles writing the codes for the following exercise -

Write a C++ program that does the following.

  1. Asks the user to enter some positive integers.

  2. Reads the positive integers from the user.

  3. Stops reading the positive integers once a non-positive integer is entered.

  4. Reports the sum of all the positive numbers that it read (exclude the negative number from the sum).

For example:

Enter some positive integers: 1 2 3 -10

The sum is: 6

This is what i have done so far. Need a clue

int a, b, c, d;
cout << "Enter some postive integers & I'll print the sum: ";
cin >> a,b,c,d;

if (a < 0)
    cout << "The sum is: " << 0 << endl;

if (b < 0)   
    cout << "The sum is: " << a << endl;

if (c < 0)
    cout << "The sum is: " << a + b << endl;

if (d < 0)
    cout << "The sum is: " << a + b + c << endl;

or should i use the while loop.

int a, b, c, d;
cout << "Enter some postive integers & I'll print the sum: ";
cin >> a,b,c,d;
while ( (a,b,c,d) < 0)  
{
      if (a < 0)
         cout << "The sum is: " << 0 << endl;

      if (b < 0)   
         cout << "The sum is: " << a << endl;

      if (c < 0)
         cout << "The sum is: " << a + b << endl;

      if (d < 0)
         cout << "The sum is: " << a + b + c << endl;        
}
while ( (a,b,c,d) > 0)
{      
         cout << "The sum is: " << a + b + c + d << endl;  
}

Edited 4 Years Ago by bdl365: wrong input

You will want to use an array and a while loop to take in the input. to get the sum you would go through the array with a for loop and add all of the values.

Why use an array? Just sum the numbers as they are entered then print the result when finished, no need to keep them.

start of loop
   enter a number
   if number less than 0?  if yes then exit the loop
   sum = sum + value of number entered
end of loop
print value of sum

Edited 4 Years Ago by Ancient Dragon

Comments
Thanks. I over thought that

Do you need to save the numbers as they are entered?
If not, you only need one input variable and one sum variable.

Also, will you always take in only four variables, or an unknown number of variables?

I would do something similar to the following:

int a, isum = 0;
cout << "Enter some postive integers & I'll print the sum: ";
cin >> a;      
while ( a > 0) {
 isum += a;
 cin >> a;
 }
cout << "The sum is: " << isum << endl;

the example that you've shown is not what i want...i need a clue on how to stop taking inout from the user as soon as the user puts in a negative number and print a sum of the numbers that the user entered excluding the negative number

please look at my code and tell me what i should have done

I posted sudo code that accomplishes that. And DavidB posted a complete program. What more do you want? Maybe you just don't understand what we posted. Put David's program into your compiler's editor, compile it, then run it to find out for yourself what it does. Then compare the program you posted with David's.

Edited 4 Years Ago by Ancient Dragon

This question has already been answered. Start a new discussion instead.