Need help with this C++ (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");

}




You almost had it right. Here is my version:
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
#include <iostream>

using namespace std;

int main (void)
{
	int sub_total=0, input=0;
	int total=0;

	while(1)
	{
		cin >> input;
		sub_total+=input;
		if (input==0)
		{
			if (sub_total==0)
			{
				cout << "total " << total << endl;
				break;
			}
			else
			{
				cout << "subtotal " << sub_total << endl;
				total+=sub_total;
				sub_total=0;
			}
		}
	}

	system("pause");
	return 0;
}

Analyze and see what you did wrong.
Last edited on
Ah, I see now. I also remembered that I could have added a counter in my code. I tested out your version as well and this also works well. Thank you very much for your explanation. =)
Topic archived. No new replies allowed.