Retaining data after restarting program?

Hi,

I'm writing a program to calculate cash transactions. I'm supposed to allow the user to restart the program to enter a new transaction an unlimited number of times. However, when the user wants to terminate it, I'm supposed to display the average amount of the transactions entered, as well as the highest and smallest amounts. I've used a do while loop to allow the user to restart the program and enter new information, but will the data store every time the program is restarted or should I assign new variables and how would I do that in the same do while loop? Thanks for the help!
I think you are supposed to allow the user to loop the user input and terminate the program when some sentinel is reached.

If you are truly supposed to let them restart the application, then you would need to write this data to disk (file) and load it when you start the program. Have you even talked about file handling yet in this class? If that is not the case, you are probably supposed to just loop.
I'm fairly certain I just need to loop the input, but my question is, if everything they input is in the do while loop, how will the program save each individual transaction, rather than just replace the assigned variable?? For example:

User inputs $500

Would you like to start again?

User inputs yes

User inputs $300

Would you like to start again?

User inputs No

Program outputs (supposed to):
# of transactions: 2
Average amount: $400
Highest: $500
Lowest: $300
closed account (S6k9GNh0)
Using terminology like "restarting" is greatly misleading. You're not stopping and then starting the program.

Moving onto the problem, in this area is all about the "scope" of where it is declared. Your problem is that you declare a variable inside of a loop and the variable is reset to garbage (or whatever you initialize it to) each iteration.

Why do you need to declare that variable in the scope of the loop?
how will the program save each individual transaction
Why would you need to do that? In your output you are not displaying each transaction. You are displaying a min, max and average, all of which are calculated by the users input.

1
2
3
4
5
6
7
8
9
10
11
12
float min, max, average;
//... init variables as needed

do
{
  //get user input..
  //calc min based of input...
  //calc max based off input..
  //calc average base off input..
}while() //some condition

//now output min, max, average 
I'm sorry if I wasn't clear. At the end of the program, I want to display how many transactions they entered, as well as the min, max, and average. If my whole set of code is one big do-while loop, how would it know how many transactions were entered or if any were entered outside of the range?
See what I typed above, notice... float min, max, average; are outside of the loop, which means they will still be in scope when you've entered the loop and when you exit the loop. If you need a count of how many transactions you've processed, create a variable, increment it with each loop, after the loop display. Just look at the code block and comments, try to do each step one at a time.
Topic archived. No new replies allowed.