Skip to content

Instantly share code, notes, and snippets.

@Nikitaw99
Last active August 22, 2016 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nikitaw99/bceefec2262e3da3413082b9aa30a701 to your computer and use it in GitHub Desktop.
Save Nikitaw99/bceefec2262e3da3413082b9aa30a701 to your computer and use it in GitHub Desktop.
#include <iostream>
int getUserInput() {
std::cout << "Please input an integer: ";
int value;
std::cin >> value;
return value;
}
int getMathematicalOperation() {
std::cout << "Please enter which operator you want. (1 = +, 2 = -, 3 = *, 4 = /): ";
int op;
std::cin >> op;
// What if the user enters an invalid character?
// We'll ignore this possibility for now
return op;
}
int calculateResult(int x, int op, int y) {
switch(op){
case 1:
return x + y;
case 2:
return x - y;
case 3:
return x * y;
case 4:
return x / y;
}
return -1;
}
void printResult(int result) {
std::cout << "Your result is: " << result << std::endl;
}
int main()
{
// Get first number from user.
int input1 = getUserInput();
// Get mathematical operator from user.
int op = getMathematicalOperation();
// Get second number from user.
int input2 = getUserInput();
// Calculate Result.
int result = calculateResult(input1, op, input2);
printResult(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment