Write a program that simulates an adding machine. When a zero is entered it shou
ID: 3629851 • Letter: W
Question
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
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int main()
{
int sub=0,tot=0,n,prev=-1;
cout<<"start the adding machine ";
cin>>n;
while(!(n==0&&prev==0))
{if(n==0)
{cout<<"subtotal "<<sub<<endl;
tot+=sub;
sub=0;
}
else
sub+=n;
prev=n;
cin>>n;
}
cout<<"total "<<tot<<endl;
system("pause");
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.