# Thread: Making a simple calculator

1. ## 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. 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.

3. post the errors you got next time.

4. 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. 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. Yeah, I didn't think div was in the std namespace. It's still good advice though.

7. 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.

8. ah my bad. div() is not under std::. sorry for the confusion.

still avoid "using namespace std;" though.

9. 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. 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. Oops, sorry guys I got it! I did have to rename "div". A simple rename to "divi" fixed it, thanks for the help!

12. 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. 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;
}```