Write a C++ program that will input only the positive numbers from the user in a loop.
If the user inputs the negative number then the loop will terminate.
Display the sum and the count of the positive numbers.
My try to solve the question but its all wrong and im lost.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
int i, counter = 0, sum = 0;
while(counter != 3)
{
cout << "Enter a number:";
cin >> i;
if( i > 0) sum += i;
else counter++;
cout << "Sum = " << sum;
cout<<" Count= "<< count;
}
return 0;
}
Sample Output:
Enter a number: 2
Enter a number: 4
Enter a number: 8
Enter a number: -2
while (true)
{
cout << "Enter a number - negative to quit ";
cin >> num;
if (num > 0)
{
count++;
sum += num;
}
elseif (num < 0)
break;
// 0 will be ignored
}