"Program that computes a running sum of inputs from the user"

This is a practice problem from a book, and I would like some pointers in the right direction. This problem is from a chapter that introduces loops, which I believe I am totally fine with.

I understand the question, and I think I know which loop to use, but I am having trouble displaying the correct number every time the sum changes.

My code looks like this so far:
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
{
  double x;
  double y;
  double z;
  double answer;

  cout <<"Please enter a number. \n";
  cin >>x;

  cout <<"Please enter another number. \n";
  cin >>y;

  answer = x + y
  cout << x << " + " << y << " = " << answer << '\n';

  while ( z != 0 )
    {
      cout << "And another. (enter 0 to terminate) \n";
      cin >> z;

      cout << answer << " + " << z << " = " << answer + z << '\n';
      /* At this point I hope you can see my problem. 'Answer' will always be
         the total from before the loop, but I want it to change every time a new
         number is added to it. */
    }
}
  
1
2
3
4
5
6
7
8
9
 while ( z != 0 )
    {
      cout << "And another. (enter 0 to terminate) \n";
      cin >> z;

      cout << answer << " + " << z << " = " << answer + z << '\n';

       answer=answer+z; // add  'z' to 'answer' to keep the total sum
    }
Try this..

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
32
33
34
35
36
37
38
#include <iostream>
using std::cout;
using std::cin;
using std::endl;


int main(){

double
        x,
        y,
        z,
        answer;

cout<<"Enter number: ";
cin>>x;
cout<<"Enter another number: ";
cin>>y;

answer=x+y;

cout<<x<<" + "<<y<<"="<<answer<<'\n'<<endl;

cout<<"And another.(enter 0 to exit): ";
cin>>z;

while(z!=0){
cout<<answer<<" + "<<z<<"="<<answer+z<<endl;;
answer+=z;
cout<<"And another.(enter 0 to quit): ";
cin>>z;
}//end while

//type your favorite pause statement HERE i.e "cin.ignore();" or whatever you want

return 0; //indicate successful termination
}//end main
Last edited on
Thank you both, problem solved
Whatisjeff, you can also do this with just 2 integers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main ()
{
    cout <<"This program will give you a runing sum \n\n";
    cout <<"of all numbers entered.\n";
    int i=0,j;

while (j !=0)
    {cout<<"\nPlease enter a number: ";
    cin >> j;
    cout<<"The total is now: "<<i+j<<"\n\n";
    i=i+j;
    }
   return 0;
}

I was stuck on this exercise for quite a while, but eventually did it for each loop type as the tutorial suggested!!! Cheers, Don
Last edited on
Thank you, Donnie. That certainly makes it more concise.

Now, I took a liking to this solution:

1
2
3
4
5
6
7
8
9
while ( z != 0 )
    {
      cout << "And another. (enter 0 to terminate) \n";
      cin >> z;

      cout << answer << " + " << z << " = " << answer + z << '\n';

       answer=answer+z; // add  'z' to 'answer' to keep the total sum
    }



It worked perfectly, but it made me wonder. If answer is answer = answer + z, and the user inputs a new value for z at the beginning of the loop, then why doesn't answer end up becoming answer + past z + current z when it is reintroduced in the cout? Please tell me if my question doesn't make any sense.
Last edited on
maybe doing this??

1
2
3
4
5
6
7
8
9
10
11
12
13
while ( z != 0 )
    {
      cout << "And another. (enter 0 to terminate) \n";
      cin >> z;

       answer=answer+z; // add  'z' to 'answer' to keep the total sum

      cout << answer << " + " << z << " = " << answer + z << '\n';

      
    }

Thank you but my original problem has already been solved. I'm just wondering if someone can answer my new question.
Ok so.. i just don't understand what you want to do! :P
Topic archived. No new replies allowed.