Thread: Help w/ program

  1. #1
    Newbie96
    Guest

    Help w/ program

    I am attempting to create a simple integer calculator that supports addition, subtraction, multiplication, modulus, and division.
    The calculator contains an Accumulator that stores the current result.
    Once started, the Accumulator is set to zero, and the operation to be performed is set to addition (+). The program then repeats the following steps:


    Request and Accept a number
    Apply the stored operation )
    Request and Accept the next operation
    This process repeats until the equals sign (=) is entered as an operation. At this point, the Accumulator contents are displayed and the calculator is reset (Accumulator set to zero and operation to +).

    The ! is treated the same as =, but causes the program to terminate.
    Any other characters entered as operations should cause a brief error message

    It suppose to run like this:
    ------
    Input: 10
    Operation: *
    Input: 2
    Operation +
    Input: -5
    Operation: =
    Result: 15
    ------
    Input: 12
    Operation: /
    Input: 0
    **Division by zero - start over
    ------
    Input: 12
    Operation: >
    **Invalid operation - ignored
    Operation: /
    Input: 5
    Operation: =
    Result: 2
    ------
    Input: 10
    Operation: %
    Input: 6
    Operation: *
    Input: 3
    Operation: !
    Result: 12
    ------

    I can't get the program to work correctly. Any help would be greatly appreciated.

    the following is my code:

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int value=0;
    	char oper_and;
    
        // input the loop count
        int accumulator = 0;
        
    	cout << "Welcome to CalcWiz\n";
    
         cout << "Enter number: ";
    	 cin>>value;
    	 accumulator +=value;
    		
    
    
        // loop "forever"
        while (oper_and != '!')
        {
            // fetch a number
    		cout << "Enter operand:";
    		cin>>oper_and;
    	
           
    
            if (oper_and == '!')
            {
                // ...then exit
                break;
            }
    
    
    		cout << "Enter number:";
    		cin>>value;
    
    	
    
            // ...otherwise add the number to the
            // accumulator
    		switch (oper_and)
    		{
    			case '+':
    				accumulator += value;
    			case '-':
    				accumulator -= value;
    			case '*':
    				accumulator *= value;
    			case '/':
    				accumulator /= value;
    			case '%':
    				accumulator %= value;
    			if (value == 0) {
                    cout << "Error: Divide by zero\n";
                    cout << "   operation ignored\n";
                } else
    		       accumulator /= value;
    			case '=':
    				cout<<"The result is:"<<accumulator;
    				accumulator = 0;
    			default:
    				cout<<"\nDIDN'T RECOGNIZE THAT!"<<endl;
    		}
        }
    
       
        // output the accumulated result
        cout << "\nThe total is " 
             << accumulator 
             << "\n";
    
        return 0;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What is not working. Can you be a little more specific on what errors you get.
    One thing I noted was that the check for division by 0 should be in the division case too.
    And you don't have any breaks in your switch.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Newbie96
    Guest

    revised-plz help

    My error comes, when I input =.
    1) I don't get a result, it ask me for another another number, than displays results.
    2) My accumulator does not reset, so my numbers keep accumulating.

    thanx for the help!

    this is the revised code:

    [code]
    // loop "forever"
    while (oper_and != '!')
    {
    // fetch a number
    cout << "Enter operand:";
    cin>>oper_and;



    if (oper_and == '!')
    {
    // ...then exit
    break;
    }


    cout << "Enter number:";
    cin>>value;



    // ...otherwise add the number to the
    // accumulator
    switch (oper_and)
    {
    case '+':
    accumulator += value;
    break;
    case '-':
    accumulator -= value;
    break;
    case '*':
    accumulator *= value;
    break;
    case '/':
    accumulator /= value;
    if (value == 0) {
    cout << "Error: Divide by zero\n";
    cout << " operation ignored\n";
    } else
    accumulator /= value;
    break;
    case '%':
    accumulator %= value;
    break;

    case '=':
    cout<<"The result is:"<<accumulator;
    accumulator = 0;
    break;
    default:
    cout<<"\nDIDN'T RECOGNIZE THAT!"<<endl;
    }
    }


    // output the accumulated result
    cout << "\nThe total is "
    << accumulator
    << "\n";

    return 0;
    }
    [\code]

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    // fetch a number
    cout << "Enter operand: ";
    cin>>oper_and;
    
    if(oper_and == '!')
    {
       // ...then exit
       break;
    }
    
    cout << "Enter number:";
    cin>>value;
    I see no reason why it shouldn't ask for a number considering those two input statements follow each other...
    If you don't want it to ask for a number, use an if(oper_and != '=') around the number input.

    PS: Why oper_and instead of operand?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM