Hello! I am a student taking an intro to programming class and I am completely LOST. The assignment is to write a program that simulates an adding machine. I have been working on this for 3 hours and so far I have only been able to get the first line "Enter an integer to add" and then I enter a 1, after that I have nothing. Earlier I got the program to run automatically adding 1 to each line and it was going crazy so I had to stop the program. Can someone help me understand how to do looping for this program? I would greatly appreciate it! Thanks! Pam

Here are the assignment instructions:

"Input to the program will be integers, submitted one per line. Input should be handled as follows: a nonzero value should be added into a subtotal; a zero value should cause the subtotal to be printed and reset to zero; two consecutive zeroes should cause the total of all the values input to be printed and the program to be terminated.
Here is sample output for the game, with the input prompt in blue, the human response in red, and the results in green:

Enter an integer to add: 5
Enter an integer to add: 6
Enter an integer to add: 0
Subtotal: 11

Enter an integer to add: -3
Enter an integer to add: -2
Enter an integer to add: 5
Enter an integer to add: 0
Subtotal: 0
Enter an integer to add: 13
Enter an integer to add: -13
Enter an integer to add: 8
Enter an integer to add: 0
Subtotal: 8

Enter an integer to add: 0
Total: 19

Also, don’t forget test your program on input that consists of only two zeroes—the output should be a 0 subtotal and a 0 total. Here's an example:

Enter an integer to add: 0
Subtotal: 0
Enter an integer to add:0
Total: 0 "

HERE IS WHAT I HAVE SO FAR, THANKS SO MUCH!!

#include <iostream>
#include <string>
using namespace std;
int main()
{
int number;
cout <<"Enter an integer to add: ";
cin>> number;
cin.ignore (1000,10);
while (number != 0)
{
std:: cout << "Enter an integer to add:";
cin>> number;
cin.ignore (1000,10);
while (number != 0)


}
std::cout << "Total. number: " << number;


return 0;
}

Edited 3 Years Ago by happygeek: fixed formatting

How about a bit of modifications:

int main()
{
    long number = 0, total = 0, subtotal = 0 ;
    bool zero_occured = false ;

    while ( true )
    {
        std:: cout << "\nEnter an integer to add: ";
        cin>> number;
        cin.ignore (1000,10);

        if( number == 0 )
        {
            if( zero_occured == false )
            {
                zero_occured = true ;
                std::cout << "\nSubtotal: " << subtotal ;
                subtotal = 0 ;
            }
            else
            {
                std::cout << "\nTotal: " << total ;
                break ;
            }
        }
        else
        {
            subtotal += number ;
            total += number ;
        }
    }
    return 0;
}

HERE IS WHAT I HAVE SO FAR, THANKS SO MUCH!!

#include <iostream>
#include <string>
using namespace std; 
int main()
{
  int number;
  cout <<"Enter an integer to add: "; 
  cin>> number; 
  cin.ignore (1000,10);
  while (number != 0)
  { 
       std:: cout << "Enter an integer to add:";
       cin>> number; 
       cin.ignore (1000,10);
       while (number != 0)
        
   }
   std::cout << "Total. number: " << number;
  
  return 0; 
}

Assuming you want to learn what's wrong with your code and not just let someone else do your homework for you:

Where's the end of your first while?
What's the second while for?
Where are you adding the numbers entered to a total?
Where are your code tags so we can read your code? (see FAQ)

How about a bit of modifications:

A bit of modification? I hope you get a good grade on it...

Edited 3 Years Ago by pyTony: fixed formatting

Assuming you want to learn what's wrong with your code and not just let someone else do your homework for you

Heh, just saw that he had made an effort so he deserved a helping hand. Hmm.. will have to keep giving out solutions under control otherwise I will get a load of PM's. Thanks for the "gentle" nudge.

This article has been dead for over six months. Start a new discussion instead.