lwschan
lwschan

Reputation: 431

How to sum the total in a loop? And pass it to another case?

In this program, I prompt the user to enter the product code and the quantity. I use a do...while loop to show it. How to I sum the all totals of every loop? For example, the user enter the code as 1, quantity as 2, the the program will calculate the total, and remember it. The next time the user enter another code and quantity, the new total will be the old total + the current total.

Upvotes: 0

Views: 131

Answers (1)

Eric J.
Eric J.

Reputation: 150108

Right under where you declare the option variable, add a total variable

int option = 0;
int total = 0;

Then right under where you get the additional quantity, also increment the total

scanf_s("%d", &quantity);
total += quantity;

Upvotes: 3

Related Questions