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 add(int x, int y);
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 << "1. Add\n";
cout << "2. Subtract\n";
cout << "3. Multiply\n";
cout << "4. Divide \n";
cout << "5. Exit menu\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;
}
int add(int x, int 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;
cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
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 I ran the solution with and it still worked perfectly fine. Why is that and why do they use 'c'?