# Thread: Please help with this calculator

1. ## 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. 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. Because it abstracts the calculations from the presentation, and makes the code more modular.

4. 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. 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. to me it just looks sloppy...the same code can be done with less function calls which makes it faster also

7. 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. 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. Oh god, you're right. Thanks for the help, and I can't believe I left something so simple out.

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

Popular pages Recent additions