View difference between Paste ID: gDviLnqW and 95GwtFtP
SHOW: | | - or go back to the newest paste.
1
/* Changes relative to original post marked with comments
2
 * Original version at http://pastebin.com/95GwtFtP
3-
using namespace std;
3+
 */
4
#include <iostream>
5-
int x;
5+
/* don't import whole namespace with 'using namespace std;'
6-
int y;
6+
 * just select the parts you need
7-
int d;
7+
 */
8-
string op;
8+
using std::cin;
9-
int answer;
9+
using std::cout;
10
using std::endl;
11
/* include string header because string is used
12
 * was previously included indirectly via iostream
13-
if (op == "Mult" || "mult" || "MULT"){
13+
 */
14-
 d = x * y;
14+
#include <string>
15-
}else if (op == "Div" || "div" || "DIV"){
15+
using std::string;
16-
 d = x / y;
16+
/* new dependency. See calc */
17-
}else if (op == "Add" || "add" || "ADD"){
17+
#include <cctype>
18-
 d = x + y;
18+
using std::tolower;
19-
}else if (op == "Sub" || "sub" || "SUB"){
19+
20-
 d = x - y;
20+
/* got rid of all global variables */
21-
}else{
21+
22-
cout << "Error";
22+
23-
}
23+
  /* convert string to lower case to simplify string matching */
24-
return d;
24+
  for(int i = 0; i < op.length(); ++i)
25
    op[i] = tolower(op[i]);
26
  int d = 0;
27-
void printanswer() {
27+
  if (op == "mult")
28-
answer = calc(op, x, y);
28+
    d = x * y;
29-
cout << "Answer:" << answer;
29+
  else if (op == "div")
30-
cout << "Parameters: " << op << x << y;
30+
    d = x / y;
31
  else if (op == "add")
32-
   
32+
    d = x + y;
33
  else if (op == "sub")
34
    d = x - y;
35-
cout << "What Operation? (Mult, Div, Sub, Add)";
35+
  else
36-
cin >> op;
36+
    cout << "Error";
37-
cout << "First Number?";
37+
38-
cin >> x;
38+
  return d;
39-
cout << "Second Number?";
39+
40-
cin >> y;
40+
41-
printanswer();
41+
void printanswer(string op, int x, int y) {
42
  int answer = calc(op, x, y);
43
  cout << "Answer: " << answer;
44
  /* add missing whitespaces between values */
45
  cout << " Parameters: " << op << " " << x << " " << y;
46
  /* add a line break */
47
  cout << endl;
48
} 
49
       
50
int main() {
51
  string op;
52
  int x, y;
53
  cout << "What Operation? (Mult, Div, Sub, Add)";
54
  cin >> op;
55
  cout << "First Number?";
56
  cin >> x;
57
  cout << "Second Number?";
58
  cin >> y;
59
  printanswer(op, x, y);
60
  /* add return statement. Value 0 signals normal termination */
61
  return 0;
62
}