Need help with this C++ adding/loop Assignment (Almost done)

Hey everyone,

I need help with this C++ assignment. I have most of it completed its just I can't get some parts to work. Here is the instructions: (My problems will be explained after the instructions.)

Write a program that simulates an adding machine. When a zero is entered it should print the subtotal of all the numbers entered from the last zero that was entered and reset the subtotal. When two consecutive zeroes are entered it should print the total (not the subtotal) of all the numbers entered and terminate the program. Example:

1
2
3
0
subtotal 6
4
5
-2
0
subtotal 7
8
0
subtotal 8
0
total 21

Be careful this program needs a bit more thought than you might think at first. To get full credit you must make sure it also works for the 0 - 0 case. Example:

0
subtotal 0
0
total 0

The problem is, after I enter the integers and type 0, it shows the subtotal which is what I want; however, when I type more integers and type another 0 to see the subtotal again, it shows the total instead. The subtotal should reset whenever a single 0 is typed and the total should only show when two 0's are inputted simultaneously. Also, after the user enters two 0's simultaneously and views their total, I want the program to exit by saying "press any key to exit." Is there a special name for that to happen? I'm still new to C++ so any help will be appreciated. I've been working on this for hours and am kind of frustrated. Thanks so much and here is my code:



#include "stdafx.h"
#include <iostream>
using namespace std;



int main()

{
    int subtotal = 0, total = 0, number = 0;

    bool input_zero = false;

    while (true)

    {
        cin >> number;

        if (number == 0)

        {

            if (input_zero == false)

            {
                input_zero = true;

                cout << "Subtotal " << subtotal << endl;

                subtotal = 0 ;
            }

            else

            {
                cout << "Total " << total << endl;
                
            }

        }

        else

        {
            subtotal += number;

            total += number;

        }

    }

    return 0;
   
	system("pause");

}
Hi, I think you will need another variable to count the number of zeros entered, and this counter will reset when a non-zero is entered.

For example:
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
int main()
{
	int subtotal = 0, total = 0, number = 0;
	int zero_count = 0;	//count the number of zero entered
	
	while (true)
    {
        cin >> number;
        if (number == 0)
        {
			zero_count = zero_count+1; // Add the counter
            if (zero_count == 1)
            {
                cout << "Subtotal " << subtotal << endl;
                subtotal = 0 ;
            }
			else if(zero_count == 2)
			{
            	cout << "Total " << total << endl; 
				break;	//Get out of the while loop
			}
		}
        else
        {	
			zero_count = 0 ; // Reset the counter
            subtotal += number;
            total += number;
        }
    }
	
	system("pause"); //System will pause before exit
	return 0;
}

Hope this helps!!!
Oh, wow. I knew I forgot something, can't believe I didn't add in the counter haha. Thanks so much for your explanation, I really appreciate it =) I probably would have been stuck on this forever if it wasn't for you. Thanks again. =)
zero_count = zero_count+1; can be also written as zero_count += 1;
Topic archived. No new replies allowed.