Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Changes relative to original post marked with comments
- * Original version at http://pastebin.com/95GwtFtP
- */
- #include <iostream>
- /* don't import whole namespace with 'using namespace std;'
- * just select the parts you need
- */
- using std::cin;
- using std::cout;
- using std::endl;
- /* include string header because string is used
- * was previously included indirectly via iostream
- */
- #include <string>
- using std::string;
- /* new dependency. See calc */
- #include <cctype>
- using std::tolower;
- /* got rid of all global variables */
- int calc (string op, int x, int y) {
- /* convert string to lower case to simplify string matching */
- for(int i = 0; i < op.length(); ++i)
- op[i] = tolower(op[i]);
- int d = 0;
- if (op == "mult")
- d = x * y;
- else if (op == "div")
- d = x / y;
- else if (op == "add")
- d = x + y;
- else if (op == "sub")
- d = x - y;
- else
- cout << "Error";
- return d;
- }
- void printanswer(string op, int x, int y) {
- int answer = calc(op, x, y);
- cout << "Answer: " << answer;
- /* add missing whitespaces between values */
- cout << " Parameters: " << op << " " << x << " " << y;
- /* add a line break */
- cout << endl;
- }
- int main() {
- string op;
- int x, y;
- cout << "What Operation? (Mult, Div, Sub, Add)";
- cin >> op;
- cout << "First Number?";
- cin >> x;
- cout << "Second Number?";
- cin >> y;
- printanswer(op, x, y);
- /* add return statement. Value 0 signals normal termination */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment