Please help with this calculator

This is a discussion on Please help with this calculator within the C++ Programming forums, part of the General Programming Boards category; Well, it calculates, it just won't loop, or exit right. . . I've tried a lot of things, I just ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Please help with this calculator

    Well, it calculates, it just won't loop, or exit right. . .
    I've tried a lot of things, I just don't know why it won't work right. So please help me.
    Code:
    #include <iostream>
    
    	int addition ( int x, int y )
    	{
    		return x + y;
    	}
    
    	int subtraction ( int x, int y )
    	{
    		return x - y;
    	}
    
    	int multiplication ( int x, int y )
    	{
    		return x * y;
    	}
    
    	int division ( int x, int y )
    	{
    		return x / y;
    	}
    
    using namespace std;
    
    int add();
    int sub();
    int mul();
    int div();
    int main()
    {
    	int input, z;
    	z = 0;
    	do
    	{
    		cout<<"1. Add\n2. Subtract\n3. Multiply\n4. Divide\nE. Exit\nEnter your choice: ";
    		cin>> input;
    			switch ( input ) {
    
    			case 1:
    				add();
    			break;
    
    			case 2:
    				sub();
    			break;
    
    			case 3:
    				mul();
    			break;
    
    			case 4:
    				div();
    			break;
    
    			case 'E':
    				( z + 1 );
    			break;
    
    			default:
    				cout<<"Error: Unknown Input\n";
    			break;
    			}
    		return 0;
        } while ( z != 1 );
    }
    	int sub(){
    	int x, y;
    		cout<<"Enter the first number to subtract: ";
    			cin>> x;
    			cin.ignore();
    		cout<<"Enter the second number to subtract: ";
    			cin>> y;
    			cin.ignore();
    		cout<<"The difference is: "<<subtraction ( x , y );
    		cout<<"\n";
    		return 0;
    	}
    
    	int add(){
    	int x, y;
    		cout<<"Enter the first number to add: ";
    			cin>> x;
    			cin.ignore();
    		cout<<"Enter the second number to add: ";
    			cin>> y;
    			cin.ignore();
    		cout<<"The sum is: "<<addition ( x, y );
    		cout<<"\n";
    		return 0;
    	}
    
    	int mul(){
    	int x, y;
    		cout<<"Enter the first number to be multiplied: ";
    			cin>> x;
    			cin.ignore();
    		cout<<"Enter the second number to be multiplied: ";
    			cin>> y;
    			cin.ignore();
    		cout<<"The product is: "<<multiplication ( x, y );
    		cout<<"\n";
    		return 0;
    	}
    
    	int div(){
    	int x, y;
    		cout<<"Enter the first number to be divided: ";
    			cin>> x;
    			cin.ignore();
    		cout<<"Enter the second number to be divided: ";
    			cin>> y;
    			cin.ignore();
    		cout<<"The quotiant is: "<<division ( x, y );
    		cout<<"\n";
    		return 0;
    	}
    When I make Exit '5' instead of 'E' it works, but it says, "Press any key to continue." I have tried the 'while' loop, but when I do it I get a black screen. I don't know why it's not working, so please help me with this.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    why have your switch call a function that calls another function...why not just have the function called in switch do the work itself? I know that's not your problem, I'm just curious

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Because it abstracts the calculations from the presentation, and makes the code more modular.

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Code:
    #include <iostream>
    
    	int addition ( int x, int y )
    	{
    		return x + y;
    	}
    
    	int subtraction ( int x, int y )
    	{
    		return x - y;
    	}
    
    	int multiplication ( int x, int y )
    	{
    		return x * y;
    	}
    
    	int division ( int x, int y )
    	{
    		return x / y;
    	}
    
    using namespace std;
    
    int add();
    int sub();
    int mul();
    int div();
    int main()
    {
    	int input, z;
    	z = 0;
    	do
    	{
    		cout<<"1. Add\n2. Subtract\n3. Multiply\n4. Divide\nE. Exit\nEnter your choice: ";
    		cin>> input;
    			switch ( input ) {
    
    			case 1:
    				add();
    			break;
    
    			case 2:
    				sub();
    			break;
    
    			case 3:
    				mul();
    			break;
    
    			case 4:
    				div();
    			break;
    
    			case 'E':
     // Notice the difference:
    
    z = z + 1;
    
     //you never assigned z to be z + 1 
     			break;
    
    			default:
    				cout<<"Error: Unknown Input\n";
    			break;
    			}
    		return 0;
        } while ( z != 1 );
    }
    	int sub(){
    	int x, y;
    		cout<<"Enter the first number to subtract: ";
    			cin>> x;
    			cin.ignore();
    		cout<<"Enter the second number to subtract: ";
    			cin>> y;
    			cin.ignore();
    		cout<<"The difference is: "<<subtraction ( x , y );
    		cout<<"\n";
    		return 0;
    	}
    
    	int add(){
    	int x, y;
    		cout<<"Enter the first number to add: ";
    			cin>> x;
    			cin.ignore();
    		cout<<"Enter the second number to add: ";
    			cin>> y;
    			cin.ignore();
    		cout<<"The sum is: "<<addition ( x, y );
    		cout<<"\n";
    		return 0;
    	}
    
    	int mul(){
    	int x, y;
    		cout<<"Enter the first number to be multiplied: ";
    			cin>> x;
    			cin.ignore();
    		cout<<"Enter the second number to be multiplied: ";
    			cin>> y;
    			cin.ignore();
    		cout<<"The product is: "<<multiplication ( x, y );
    		cout<<"\n";
    		return 0;
    	}
    
    	int div(){
    	int x, y;
    		cout<<"Enter the first number to be divided: ";
    			cin>> x;
    			cin.ignore();
    		cout<<"Enter the second number to be divided: ";
    			cin>> y;
    			cin.ignore();
    		cout<<"The quotiant is: "<<division ( x, y );
    		cout<<"\n";
    		return 0;
    	}

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    An example of the reworked code (especially reworked for readability...hint hint)

    Code:
    #include <iostream>
    using namespace std;
    
    void add();
    
    int main()
    {
    	int input, z;
    	z = 0;
    	do
    	{
    		cout<<"1. Add\n2. Subtract\n3. Multiply\n4. Divide\nE. Exit\nEnter your choice: ";
    		cin>> input;
    		switch ( input ) 
    		{
    		case 1:
    			add();
    			break;
    		default:
    			cout<<"Error: Unknown Input\n";
    			break;
    		}
        } while ( z != 1 );
    return 0;
    }
    
    //function add() definition 
    //function is now self contained
    void add()
    {
    int x, y;
    	cout << "Enter the first number to add: ";
    	cin  >> x;
    	cin.ignore();
    	cout << "Enter the second number to add: ";
    	cin  >> y;
    	cin.ignore();
    	cout << "The sum is " << x + y << endl;
    }

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    to me it just looks sloppy...the same code can be done with less function calls which makes it faster also

  7. #7
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    What if he wants to make 'add()' append the records of an XML file and a database table? Sure, his add method was a simple x+y, but that doesn't mean he doesn't plan to change it.

    It's always a good thing to split functions into more functions (to some degree).

    Anyway, that wasn't the problem.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    well I just using his problem to get some clarity of my own...I see what you're saying, I'm not advanced enough to have even thought about the scenario you presented to be honest...the code just looked sloppy to me, that's all...oh and good call on the never assigning z to the new value (z+1 in this case)...it's always annoying when you forget those tiny little details like that

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Oh god, you're right. Thanks for the help, and I can't believe I left something so simple out.

  10. #10
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by Chaplin27
    the same code can be done with less function calls which makes it faster also
    Keep in mind that the sourcecode you look at is not necessarily compiled and executed precisely as you see it. In this simple example, chances are the functions were compiled inline and thus optimized for performance.

    However the practice used in the sourcecode promotes reusability which is beneficial should the program become more complex. Who knows, in the future he may opt to overload the functions to handle different datatypes.
    Last edited by Scribbler; 03-08-2005 at 01:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 01:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21