Anything unnecessary or able to be improved in this code?

This is my first post to the forum, so apologies if it's in the wrong forum or even a useless question, but I've only been coding for about a week and I was wondering if I made any unnecessary lines or if anything could be improved as I'm still getting the feel of it. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
using namespace std;

int main()

{
	string yesNo, yes="yes";
	string multiply, Multiply;
	string add, Add, addition, Addition;
	string Divide, divide;
	string Subtract, subtract;
	string which;


	int a, b;
	int remainder;
	int answer=0;

	double quotient;

	cout << "Welcome to matt\'s simple calculator!\n";

	while (answer == 0) {

	cout << "\nPlease specify a type of operation\n\n";
	cout << "Add\nMultiply\nSubtract\nDivide\n";

	cin >> which;

		if ((which == "multiply")||(which == "Multiply")) {
		cout << "\nEnter first number\n";
		cin >> a;
		cout << "\nEnter second number\n";
		cin >> b;
		cout << "\nThe product is " << a*b;
		}

			else if ((which == "add")||(which == "Add")) {
				cout << "\nEnter first number\n";
				cin >> a;
				cout << "\nEnter second number\n";
				cin >> b;
				cout << "\nThe sum of the two numbers is " << a+b;
			}

			else if ((which == "Subtract")||(which == "subtract")) {
				cout << "\nEnter number to subtract from\n";
				cin >> a;
				cout << "\nEnter number to take away from first number\n";
				cin >> b;
				cout << "\nThe difference is " << a-b;
			}

			else if ((which == "Divide")||(which == "divide")) {
				cout << "\nEnter number to divide from\n";
				cin >> a;
				cout << "\nEnter number to divide from first number\n";
				cin >> b;
				quotient = a/b;
				remainder = a%b;
				cout << "\nThe quotient is " << quotient;
					if (remainder != 0) {
						cout << " " << remainder << "/" << b;
			}
		}

		else {
			cout << "Error: unexpected input.";
		}

		cout << "\n\nWould you like to make another calculation?\n\n";
		cin >> yesNo;

			if (yesNo == yes){
				answer = 0;
			}
			else {
				answer = 1;
			}
		}
	cout << "Thank you for using matt\'s calculator!";

	return 0;

}
Last edited on
You should consider using a switch statement. It could really clean up your code.
Hi,

There are a few things here.

The biggest one is to not try and cope with every or some of the combinations of words: "Add" "addition" "Addition", what if the user types aDdition Instead stick to options which are a single char or number in a menu.

Integer math doesn't work well for this, especially division, change the type to double

When doing division, always check for division by zero. With double this is a little trickier: We need to check if the absolute value of the number is less than some arbitrary precision value, because doubles are not stored exactly.

1
2
3
4
5
6
7
8
9
10
const double Precision = 0.001;

double a = 0.1; // really 0.1 +/- 3e-16 say
double b = 1.0 - (10.0 * a); // not zero, +/- 3e-15 say

if(std::abs(b) < Precision ) {
   std::cout << "b is near enough to zero (< 0.001)\n";
} else {
   std::cout << "b is not zero \n";
}


Hope this helps :+)
Last edited on
Thanks guys I'll try all of the suggestions, have a good one.
Topic archived. No new replies allowed.