Let me start by saying that this is nice clean code for a beginner. The fact that you're reaching out for opinions shows a great willingness to learn. Go you!!
The loop in main could be simply:
1 2 3 4
|
while (start()) {
menu();
selection();
};
|
The name start() is misleading since it's run each time through the loop. Maybe keepGoing() or something like that. Whatever you call it, it would be better to return bool instead of int. This would better reflect the fact that it returns a yes/no status.
Line 39-45 could be replace with:
return (start == 1);
Any time you find yourself writing the same code several times, you should see if you can factor it out into a separate function. For example, addition(), subtraction(), multiplication() and addition() all have the same code to prompt for two numbers:
1 2 3 4 5 6 7 8
|
// Prompt for two numbers and put them in "a" and "b"
void getTwo(double &a, double &b)
{
cout << "Enter a number.\n";
cin >> a;
cout << "Enter another number.\n";
cin >> b;
}
|
Note that a and b are passed by reference.
Then addition() becomes simply:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int
addition()
{
double a, b;
double sum = 0.0;
getTwo(a,b);
sum = (a + b);
cout << "The sum is:\n";
cout << sum;
cout << "\n";
return 0;
}
|
Caution: I haven't initialized
a
and
b
because I know that any value will be overwritten in getTwo(). As a beginner, you might want to always initialize variables to avoid the common bug of using an uninitialized one.
In selection(), you should use a
switch()
statement instead of repeated if's. Not only is this potentially faster, but it better reflects that fact that you're executing different code depending on the value of n:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
switch(n) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiplication();
break;
case 4:
division();
break;
case 5:
sumofintegers();
break;
}
|
In division(), you should probably check for division by zero and print an appropriate error message if it's attempted.
Line 198 can be simplified with the += operator:
total += z;