Homework Issues

I am a beginner, preparing for midterm. Doing my own practice problems. Problem is Allow users to continuously enter numbers until they enter 0 then display the sum of the numbers entered. Here is my code, it allows me to enter 1 number and then sums it.

#include <iostream>
#include <math.h>

using namespace std;
int main ()
{
int count; //counter
int num; //user enters number
int sum = 0; //output: display sum of only positive numbers

//cout << "Please enter your numbers: . Entering a zero will quit program." << endl;


//start loop
while (count !=3) {
cout << "Please enter your numbers: . Entering a zero will quit program." << endl;
cin >> num;
//sum = sum + num;
//cout << sum << endl;
if (num > 0)
sum += num;
else
count++;
cout << "Sum is: " << sum <<endl;
}

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <math.h>

using namespace std;
int main ()
{
int count;	//counter
int num;	//user enters number
int sum = 0;	//output: display sum of only positive numbers

//cout << "Please enter your numbers: . Entering a zero will quit program." << endl;


//start loop
while (count !=3) {
cout << "Please enter your numbers: . Entering a zero will quit program." << endl;
cin >> num;
//sum = sum + num;
//cout << sum << endl;
if (num > 0)
sum += num;
else
count++;
cout << "Sum is: " << sum <<endl;

if (num==0)   //You missed this statement that breaks out of the while loop when 0 is entered and terminates the program
    break;
}

return 0;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main ()
{
    int num; //user enters number
    int sum = 0; //output: display sum of only positive numbers
    
    cout << "Please enter your numbers: . Entering a zero will quit program." << endl;
    while (cin >> num && num != 0) // <---
    {
        cout << "Please enter your numbers: . Entering a zero will quit program." << endl;
        sum += num;
        cout << "Sum is: " << sum <<endl;
    }

    return 0;
}
Last edited on
Thanks for the help, this forum does help me.
Might want to try a simple do/while loop. fewer lines, easy to read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
    int num(0), sum(0);
    cout << "Enter numbers to sum up, '0' to quit" << endl;
    do                                    // "do" guarantees the loop runs at least once
    {
        cin >> num;
        sum += num;
    }
    while( num != 0);

    cout << endl << sum << endl;
    return 0;
}
closed account (48T7M4Gy)
And that can be taken one step further:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
    int num(0), sum(0);
    
    do                                    // "do" guarantees the loop runs at least once
    {
        cout << "Enter numbers to sum up, '0' to quit and show total" << endl;
        cin >> num;
        sum += num;
    }
    while( num != 0);

    cout << endl << sum << endl;
    return 0;
}
Topic archived. No new replies allowed.