I'm very new to C++ and am trying to code simple programs to test my knowledge on the very basics of this language. Yesterday, I created a very simple calculator script which allows you to add, subtract, multiply, and divide and set of two numbers. While it is still very basic (e.g. only calculating two numbers), I plan on adding more functionality and improving the overall code-base to be not only more efficient, but a more pleasing experience to review. Remember, I am still VERY new to this and need some guidance (for example, I used goto(label) to escape nested loops, there's probably a much better way to do this) on how I can improve this code. Thanks.
main.cpp
#include <iostream>
#define log(x) std::cout << x;
#define logl(x) std::cout << x << std::endl;
int main() {
int choice, choice2;
float first, second;
logl("-----------------------");
logl("schmobbing's calculator");
logl("-----------------------");
start:
std::cout << "" << std::endl;
logl("Choose one of the following calculation options.");
log("[1] Addition, [2] Subtraction, [3] Multiplication, [4] Division: ");
std::cin >> choice;
while (true) {
switch (choice) {
case 1:
logl("");
logl("You've chosen addition.");
break;
case 2:
logl("");
logl("You've chosen subtraction.");
break;
case 3:
logl("");
logl("You've chosen multiplication.");
break;
case 4:
logl("");
logl("You've chosen division.");
break;
default:
logl("");
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("You've chosen an invalid operator, please try again.");
log("[1] Addition, [2] Subtraction, [3] Multiplication, [4] Division: ");
std::cin >> choice;
continue;
} else {
logl("You've chosen an invalid operator, please try again.");
log("[1] Addition, [2] Subtraction, [3] Multiplication, [4] Division: ");
std::cin >> choice;
continue;
}
}
break;
}
while (true) {
switch (choice) {
case 1:
while (true) {
log("Enter your first number: ");
std::cin >> first;
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid number, please try again.");
continue;
}
break;
}
while (true) {
log("Enter your second number: ");
std::cin >> second;
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid number, please try again.");
continue;
}
break;
}
logl("");
std::cout << "The sum of the given numbers is " << first + second << "." << std::endl;
while (true) {
log("Would you like to calculate for a different result? [1] Yes or [2] No: ")
std::cin >> choice2;
switch (choice2) {
case 1:
goto start;
case 2:
goto end;
default:
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid option, please try again.");
break;
} else {
logl("");
logl("You've chosen an invalid option, please try again.");
break;
}
}
}
break;
case 2:
while (true) {
log("Enter your first number: ");
std::cin >> first;
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid number, please try again.");
continue;
}
break;
}
while (true) {
log("Enter your second number: ");
std::cin >> second;
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid number, please try again.");
continue;
}
break;
}
logl("");
std::cout << "The difference of the given numbers is " << first - second << "." << std::endl;
while (true) {
log("Would you like to calculate for a different result? [1] Yes or [2] No: ")
std::cin >> choice2;
switch (choice2) {
case 1:
goto start;
case 2:
goto end;
default:
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid option, please try again.");
break;
} else {
logl("");
logl("You've chosen an invalid option, please try again.");
break;
}
}
}
break;
case 3:
while (true) {
log("Enter your first number: ");
std::cin >> first;
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid number, please try again.");
continue;
}
break;
}
while (true) {
log("Enter your second number: ");
std::cin >> second;
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid number, please try again.");
continue;
}
break;
}
logl("");
std::cout << "The product of the given numbers is " << first * second << "." << std::endl;
while (true) {
log("Would you like to calculate for a different result? [1] Yes or [2] No: ")
std::cin >> choice2;
switch (choice2) {
case 1:
goto start;
case 2:
goto end;
default:
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid option, please try again.");
break;
} else {
logl("");
logl("You've chosen an invalid option, please try again.");
break;
}
}
}
break;
case 4:
while (true) {
log("Enter your first number: ");
std::cin >> first;
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid number, please try again.");
continue;
}
break;
}
while (true) {
log("Enter your second number: ");
std::cin >> second;
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid number, please try again.");
continue;
}
break;
}
if (second == 0) {
logl("");
std::cout << "The quotient of the given numbers is undefined." << std::endl;
} else {
logl("");
std::cout << "The quotient of the given numbers is " << first / second << "." << std::endl;
}
while (true) {
log("Would you like to calculate for a different result? [1] Yes or [2] No: ")
std::cin >> choice2;
switch (choice2) {
case 1:
goto start;
case 2:
goto end;
default:
if (!std::cin) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logl("");
logl("You've chosen an invalid option, please try again.");
break;
} else {
logl("");
logl("You've chosen an invalid option, please try again.");
break;
}
}
}
}
end:
break;
}
}
```
goto
should be avoided, but if it is really clear what you are doing, like breaking from multiple nested loops, it is fine. The code is in general hard to read, try to split it into functions. Well, that is the biggest recomendation, learn to use functions. Things like the validation of the user input are copy paste, it is better to abstract it into a function. Just as something tho take into account when you learn abaut them. Give them only one clear purpose, and give then good names. \$\endgroup\$constexpr
. \$\endgroup\$