# Thread: Need help understanding the program solution for a book exercise

1. ## Need help understanding the program solution for a book exercise

The exercise ask to make a calculator program to perform computations in a separate function for each type of computation.

Here is what my solution looks like and it works perfectly fine.
Code:
```#include <iostream>

using namespace std;

// prototypes
int sub(int x, int y);
int mult(int x, int y);
int quo(int x, int y);

int main()
{
int userChoice;
int num1, num2;

do {
cout << "Which operation would you like to perform: \n";
cout << "2. Subtract\n";
cout << "3. Multiply\n";
cout << "4. Divide \n";
cin >> userChoice;

switch (userChoice) {
case 1:
cout << "Choose the first number you would like to add: \n";
cin >> num1;
cout << "Choose the second number you would like to add: \n";
cin >> num2;
cout << "The sum of the numbers is: " << add(num1, num2) << '\n';
break;
case 2:
cout << "Pick the first number you would like to subtract: \n";
cin >> num1;
cout << "Choose the second number you would like to subtract: \n";
cin >> num2;
cout << "The difrence between the two numbers is: " << sub(num1, num2) << '\n';
break;
case 3:
cout << "Choose the first nummber you would like to multiply: " << '\n';
cin >> num1;
cout << "Choose the second nummber you would like to multiply: " << '\n';
cin >> num2;
cout << "The product of the two numbers is: " << mult(num1, num2) << '\n';
break;
case 4:
cout << "Choose the first nummber you would like to divide: " << '\n';
cin >> num1;
cout << "Choose the second nummber you would like to divide: " << '\n';
cin >> num2;
cout << "The quotient of the two numbers is: " << quo(num1, num2) << '\n';
break;
default:
cout << "Exiting program...cya! \n";
break;
}
} while (userChoice != 5);// exits menu

system("pause");
return 0;
}

// returns sum
int add(int x, int y) {
return x + y;
}

// returns the difference
int sub(int x, int y) {
return x - y;
}

// returns the product
int mult(int x, int y) {
return x * y;
}

// returns the quotient
int quo(int x, int y) {
return x / y;
}```
The solution in the book looks like this:
Code:
```#include <iostream>

int multiply(int x, int y)
{
return x*y;
}

int divide(int x, int y)
{
return x/y;
}

{
return x+y;
}

int subtract(int x, int y)
{
return x-y;
}

using namespace std;

int main()
{
char op='c';
int x, y;

while(op!='e')
{
cout<<"What operation would you like to perform: add(+), subtract(-), divide(/), multiply(*), [e]xit?";cin>>op;
switch(op)
{
case '+':
cin>>x;
cin>>y;
break;

case '-':
cin>>x;
cin>>y;
cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
break;

case '/':
cin>>x;
cin>>y;

cout<<x<<"/"<<y<<"="<<divide(x,y)<<endl;
break;
case '*':
cin>>x;
cin>>y;
cout<<x<<"*"<<y<<"="<<multiply(x,y)<<endl;
break;

case 'e':
return 0;

default:
cout<<"Sorry, try again"<<endl;
}
}
return 0;
}```
What I want to know is why does the solution use
Code:
`char op = 'c';`
I ran the solution with
Code:
` char op;`
and it still worked perfectly fine. Why is that and why do they use 'c'? 2. So that
while(op!='e')
Doesn't compare with a random uninitialized value which might randomly be e. 3. Thanks for the clarification! I have one more question. Would you say the book solution is better than the solution I came up with? Is there anything I could have done to make my solution better? Sorry, if what I'm asking is a dumb question. 4. Perhaps you could add another function to replace the code you copy and pasted.

Void prompt ( int &x, int &y, string prompt) Popular pages Recent additions exercise, int, program, prototypes, solution 