So i'm having troubles writing the codes for the following exercise -
Write a C++ program that does the following.
Asks the user to enter some positive integers.
Reads the positive integers from the user.
Stops reading the positive integers once a non-positive integer is entered.
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;
}