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.
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
}