Sign in to follow this  

Maintain a running total

Recommended Posts

Im not sure how to have the function maintain a running total.
In my example it does not give the total score, but each time
runtot function is called it resets the total.

#include <iostream>
using namespace std;

int runtot (int subtot = 0);

int main()
srand (time(NULL));
int random = (rand()%10);
cout<<random<<" \n";
runtot (random);

int randm = (rand()%10);
cout<<randm<<" \n";
runtot (randm);

int total = runtot ();
cout<<total; //I want this to print out the running total of runtot()

return 0;

int runtot (int subtot)
int change = 0;
change += subtot; //I want the variable change to maintain a running total in this function
cout<<change<<" \n";

return change;

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this