Needing help with passing numbers

I am currently working on a little program that will take a positive number and add it to the previous number while keeping a running total. The exit is to input a non positive number. I have everything worked out but for some reason I cannot figure out why my new total will not output the previous total plus the next number typed in. Any help is appreciated.

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
  #include <iostream>

using namespace std;

int main ()
{
	int startnum = 0;
	int nextnum;
	int posnum;

		cout << "Enter a positive number ";

	cin >> posnum;

		cout << "Your new total is " << startnum + posnum << endl;

	while (posnum >= 0)
	{
	cout << "Please enter another positive number " << endl;
	cin >> nextnum;
	cout << "Your new total is " << posnum + nextnum << endl;
	}

		cout << endl;
		cout << "That was a negative number " << endl;
		cout << endl;

	return 0;
}
Have you learned break; yet? Also it's because you don't add the nextnum into the running total, you just display it, then loose it.
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
#include <iostream>

using namespace std;

int main ()
{
	int startnum = 0;
	int nextnum;

	while (true)
	{
		cout << "Please enter a number " << endl;
		cin >> nextnum;

		if (nextnum < 0)
			break;
		
		startnum += nextnum;
		cout << "Your new total is " << startnum << endl;
	}

	cout << endl;
	cout << "That was a negative number " << endl;
	cout << endl;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.