I'm a freshman Computer Science major looking to build projects for my portfolio. I'm new to C++ and programming in general. Is there any way I can improve this basic calculator I've made? Also, what are the next steps you would recommend considering what I've utilized in the program below? I've got a basic idea of static_cast(), cin.clear(), and cin.ignore(), but I just got the solutions from Stack Overflow.
#include <iostream>
#include <cmath>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main() {
float num1 = 0;
float num2 = 0;
float total = 0;
string operation = "a";
string choice = "a";
do {
cout << "First Number: ";
cin >> num1;
//checks if input is an int
while (cin.fail()) {
cin.clear();
cin.ignore();
cout << "Please input a number: ";
cin >> num1;
}
num1 = static_cast<float>(num1);
cout << "Operation (+, -, *, /, or ^): ";
//checks if input is a viable operation
cin >> operation;
while ((operation != "+") && (operation != "-") && (operation != "*") && (operation != "/") && (operation != "^")) {
cout << "Please input a proper operation: ";
cin >> operation;
}
cout << "Second Number: ";
cin >> num2;
//checks if input is an int
while (cin.fail()) {
cin.clear();
cin.ignore();
cout << "Please input a number: ";
cin >> num2;
}
//prevents dividing by zero
while (num2 == 0 && operation == "/") {
cout << "You can't divide by zero, silly! Try again: ";
cin >> num2;
}
num2 = static_cast<float>(num2);
//does math based on inputted opperation
if (operation == "+") {
total = num1 + num2;
}
if (operation == "-") {
total = num1 - num2;
}
if (operation == "*") {
total = num1 * num2;
}
if (operation == "/") {
total = num1 / num2;
}
if (operation == "^") {
total = pow(num1,num2);
}
cout << "Total: " << total << endl;
cout << "Would you like to calculate again? (Y/N): ";
cin >> choice;
//same
while ((choice != "Y") && (choice != "N") && (choice != "y") && (choice != "n")) {
cout << "Type a proper response (Y/N): ";
cin >> choice;
}
if ((choice == "N") || (choice == "n")) {
cout << "Thank you!";
}
} while ((choice == "Y") || (choice == "y"));
return 0;
}
"0+"\$\endgroup\$