So i´m finally done with my calculator, its working and most bugs are done with.
Now i would like to know how to tidy up the code, is there ways to make it more efficient, more user foolproof, simply better ways to code it?
// looped calculator
#include
//variables
float num1;
float num2;
float sum;
char oper;
//functions
int main()
{
//greets the user
std::cout << "welcome to the calculator\n";
//instructions on how to use the program
std::cout << "press ENTER after each input and please use only + - * / or 'q' to quit\n";
//Prompts the user to input number
std::cout << "enter number: ";
// user inputs number
while (!(std::cin >> num1))
{
std::cout << "enter a number please (enter to confirm): ";
std::cin.clear();
std::cin.ignore(100, '\n');
}
//Prompts the user to input operator or quit the program
std::cout << "enter operator or quit (enter to confirm): ";
//user inputs operator or quits
std::cin >> oper;
//Prompts the user to input number
std::cout << "enter number (enter to confirm): ";
while (!(std::cin >> num2))
{
std::cout << "enter a number please (enter to confirm): ";
std::cin.clear();
std::cin.ignore(100, '\n');
}
//switch loop depending on what operator was chosen
switch (oper)
{
case '+':
sum = num1 + num2;
break;
case '-':
sum = num1 - num2;
break;
case '*':
sum = num1 * num2;
break;
case '/':
sum = num1 / num2;
break;
default:
std::cout << "enter only valid operators + - * / (enter to confirm) \n";
break;
}
while (true)
{
//outputs the sum
std::cout << sum << std::endl;
//Prompts the user to input operator
std::cout << "enter operator or quit (enter to confirm): ";
//user inputs operator or quit the program
std::cin >> oper;
//Quits the program
if (oper == 'q')
break;
// Prompts the user to input number
std::cout << "enter number (enter to confirm): ";
while (!(std::cin >> num2))
{
std::cout << "enter a number please (enter to confirm): ";
std::cin.clear();
std::cin.ignore(100, '\n');
}
//switch loop depending on what operator was chosen
switch (oper){
case '+':
sum = sum + num2;
break;
case '-':
sum = sum - num2;
break;
case '*':
sum = sum * num2;
break;
case '/':
sum = sum / num2;
break;
default:
std::cout << "enter only valid operators + - * / (enter to confirm)\n";
break;
}
}
//outputs sum then quits the program
std::cout << sum;
std::cin.ignore();
std::cin.get();
return 0;
}
I tried making it look pretty but this is the best i could do :P
I´ll gladly accept help with how to make code look nice on here because i think i´ll post some more here in the future :D