/*
* Chapter 21 Problem 1
*
* Write a program that contains the functions add, subtract,
* multiply, and divide. Each of these functions should take
* two integers and return the results of the operation. Create
* a small calculator that uses these functions. Put the function
* declarations into a header file, but include the source code
* for these functions in your source code.
*
*/
#include <iostream>
#include "Calculator.h"
usingnamespace std;
int main()
{
// Declare and initialize variables
int x_value = 0;
int y_value = 0;
int selection = 0;
bool repeat = true;
while (repeat == true)
{
cout << "Menu: " << endl;
cout << '\t' << "0. Exit" << endl;
cout << '\t' << "1. Add" << endl;
cout << '\t' << "2. Subtract" << endl;
cout << '\t' << "3. Multiply" << endl;
cout << '\t' << "4. Divide" << endl;
cout << "Please select a number from the Menu: ";
cin >> selection;
cout << endl << "Please provide me with integer variable x: ";
cin >> x_value;
cout << "Please provide me with integer variable y: ";
cin >> y_value;
switch (selection)
{
case 0:
repeat = false;
break;
case 1:
cout << "x + y = " << AddFunction(x_value, y_value) << endl << endl;
break;
case 2:
cout << "x - y = " << SubtractFunction(x_value, y_value) << endl << endl;
break;
case 3:
cout << "x * y = " << MultiplyFunction(x_value, y_value) << endl << endl;
break;
case 4:
cout << "x / y = " << DivideFunction(x_value, y_value) << endl << endl;
break;
}
}
}
// Function Definitions
int AddFunction(int x, int y)
{
int result = 0;
return result = x + y;
}
int SubtractFunction(int x, int y)
{
int result = 0;
return result = x - y;
}
int MultiplyFunction(int x, int y)
{
int result = 0;
return result = x * y;
}
int DivideFunction(int x, int y)
{
int result = 0;
return result = x / y;
}
.
It does exit when i tried it. Of course the execution has to make its way as far as the closing brace at line 62, when it will go back to the top of the while loop, test the value of repeat, and then terminate.
If you don't want any of the code from line 39 to 42 to execute when the user selected option 0, you would need to change the logic, for example by enclosing that part of the code in an if statement