Last active
August 22, 2016 13:10
-
-
Save Nikitaw99/bceefec2262e3da3413082b9aa30a701 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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