Making a simple calculator

This is a discussion on Making a simple calculator within the C++ Programming forums, part of the General Programming Boards category; Hey I just started learning some C++ programming from the tutorials provided by this website, and I'm trying to make ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    New York
    Posts
    4

    Making a simple calculator

    Hey I just started learning some C++ programming from the tutorials provided by this website, and I'm trying to make a simple calculator. This is what I have but I'm getting build errors, can anyone help me? I can't seem to find what's wrong.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int add ( int x, int y );
    
    int div ( int x, int y );
    
    int sub ( int x, int y );
    
    int main()
    {
    	int x;
    	int y;
    	int option;
    
    	cout<<"1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide";
    	cin>> option;
    	cin.ignore();
    	
    	if ( option == 1 )
    	{
    		cout<<"Input the numbers you would like to add.\n";
    		cin>> x >> y;
    		cin.ignore();
    		cout<<"The sum is: "<< add ( x, y ) <<".\n";
    	}
    	else if ( option == 2 )
    	{
    		cout<<"Input the numbers you would like to subtract.\n";
    		cin>> x >> y;
    		cin.ignore();
    		cout<<"The difference is: "<< sub ( x, y ) <<".\n";
    	}
    	else if ( option == 3 )
    	{
    		cout<<"Input the numbers you would like to multiply.\n";
    		cin>> x >> y;
    		cin.ignore();
    		cout<<"The product is: "<< mult ( x, y ) <<".\n";
    	}
    	else if ( option == 4 )
    	{
    		cout<<"Input the numbers you would like to divide.\n";
    		cin>> x >> y;
    		cin.ignore();
    		cout<<"The quotient is: "<< div ( x, y ) <<".\n";
    	}
    	cin.get();
    }
    
    int add ( int x, int y )
    {
    	return x + y;
    }
    
    int sub ( int x, int y )
    {
    	return x - y;
    }
    
    int mult ( int x, int y )
    {
    	return x * y;
    }
    	int div ( int x, int y )
    {
    	return x / y;
    }

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    div is the name of a standard library function in cstdlib.
    Sometimes compilers automatically recognize standard library functions even if you don't include the header.
    Try renaming div.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    post the errors you got next time.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    div is the name of a standard library function in cstdlib.
    Sometimes compilers automatically recognize standard library functions even if you don't include the header.
    Try renaming div.
    I bet you have heard people saying don't write "using namespace std;" by now, and this is why.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Nah even if you're well-behaved, using a standardized function name isn't a good idea and triggers something at compile time.
    Code:
    #include <iostream>
    #include <ostream>
    
    int div ( int a, int b );
    
    int main ( )
    {
        int a = 915; 
        int b = 508;
        std::cout << div( a, b ) << std::endl;
        return 0;
    }
    
    int div ( int a, int b )
    { 
        return a / b;
    }
    
    ------ Build started: Project: learn, Configuration: Debug Win32 ------
    Compiling...
    learn.cpp
     : error C2556: 'int div(int,int)' : overloaded function differs only by return type from 'div_t div(int,int)'
            c:\program files\microsoft visual studio 8\vc\include\stdlib.h(482) : see declaration of 'div'
     : error C2371: 'div' : redefinition; different basic types
            c:\program files\microsoft visual studio 8\vc\include\stdlib.h(482) : see declaration of 'div'
    It's because the runtime depends on stdlib.h I think.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Yeah, I didn't think div was in the std namespace. It's still good advice though.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Nah even if you're well-behaved, using a standardized function name isn't a good idea and triggers something at compile time.
    Ah, but you are not well behaved enough since you chuck the function in the global namespace.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    ah my bad. div() is not under std::. sorry for the confusion.

    still avoid "using namespace std;" though.

  9. #9
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118
    I'm still a C++ novice but I know a bit about maths, I probably wouldn't use integer variables when writing a function for division.

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    New York
    Posts
    4
    Is this the error log you were looking for? Still can't figure this out...

    Code:
    Compiling...
    functions.cpp
    c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\functions.cpp(11) : error C2556: 'int div(int,int)' : overloaded function differs only by return type from 'div_t div(int,int)'
            c:\program files\microsoft visual studio 9.0\vc\include\stdlib.h(446) : see declaration of 'div'
    c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\functions.cpp(11) : error C2371: 'div' : redefinition; different basic types
            c:\program files\microsoft visual studio 9.0\vc\include\stdlib.h(446) : see declaration of 'div'
    c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\functions.cpp(49) : error C2264: 'div' : error in function definition or declaration; function not called
    c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\functions.cpp(69) : error C2556: 'int div(int,int)' : overloaded function differs only by return type from 'div_t div(int,int)'
            c:\program files\microsoft visual studio 9.0\vc\include\stdlib.h(446) : see declaration of 'div'
    c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\functions.cpp(69) : error C2491: 'div' : definition of dllimport function not allowed

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    New York
    Posts
    4
    Oops, sorry guys I got it! I did have to rename "div". A simple rename to "divi" fixed it, thanks for the help!

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    error C2371: 'div' : redefinition; different basic types
    Redefinition means a function is defined more than once. You only defined it once, so the other instance must be somewhere in the standard library (in iostream).

  13. #13
    Registered User
    Join Date
    May 2008
    Location
    New York
    Posts
    4
    Ah I see. My other question is, as soon as you put in your numbers and an answer is given, the program will shut down when you press enter. How do I make the whole cycle loop? I've tried wrapping the code in every type of loop I can think of and nothing, any ideas? Here's the current code.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int add ( int x, int y );
    
    int sub ( int x, int y );
    
    int mult ( int x, int y );
    
    int divi ( int x, int y );
    
    int main()
    {
    	int x;
    	int y;
    	int option;
    
    	cout<<"1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n\n";
    	cin>> option;
    	cin.ignore();
    	cout<<"\n";
    	
    	if ( option == 1 )
    	{
    		cout<<"Input the numbers you would like to add.\n\n";
    		cin>> x >> y;
    		cin.ignore();
    		cout<<"\nThe sum is: "<< add ( x, y ) <<".\n";
    	}
    	else if ( option == 2 )
    	{
    		cout<<"Input the numbers you would like to subtract.\n\n";
    		cin>> x >> y;
    		cin.ignore();
    		cout<<"\nThe difference is: "<< sub ( x, y ) <<".\n";
    	}
    	else if ( option == 3 )
    	{
    		cout<<"Input the numbers you would like to multiply.\n\n";
    		cin>> x >> y;
    		cin.ignore();
    		cout<<"\nThe product is: "<< mult ( x, y ) <<".\n";
    	}
    	else if ( option == 4 )
    	{
    		cout<<"Input the numbers you would like to divide.\n\n";
    		cin>> x >> y;
    		cin.ignore();
    		cout<<"\nThe quotient is: "<< divi ( x, y ) <<".\n\n";
    	}
    	cin.get();
    }
    
    int add ( int x, int y )
    {
    	return x + y;
    }
    
    int sub ( int x, int y )
    {
    	return x - y;
    }
    
    int mult ( int x, int y )
    {
    	return x * y;
    }
    int divi ( int x, int y )
    {
    	return x / y;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator - Past the basics of 2+2
    By Revekius in forum C Programming
    Replies: 11
    Last Post: 07-29-2006, 11:36 PM
  2. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 07:59 AM
  3. Making a Simple Database System
    By Speedy5 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2003, 09:17 PM
  4. Having some problem with my simple calculator
    By Aven in forum C Programming
    Replies: 3
    Last Post: 07-21-2002, 09:03 AM
  5. Replies: 7
    Last Post: 07-19-2002, 04:33 AM

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