I'm trying to do some begginer-level calculator in c++ and this got me curious, i have a switch where i compare the operator that is entered, I wanted to print out "The symbol you entered is not valid." in default at the end of the switch, but what happens is "The symbol you entered is not valid.0" and when I tried to do the same with if it prints out "The symbol you entered is not valid.-nan(ind)" **Why is that? and how does one print out text in if/switch without this happening?
class CalculatorOOP {
public:
float Sum(float a, float b) {
return a + b;
};
float Minus(float a, float b) {
return a - b;
};
float Multiply(float a, float b) {
return a * b;
};
float Divide(float a, float b) {
return a / b;
};
float ResultSwitch(char Operator, float a, float b) {
switch (Operator) {
case '+':
return Sum(a, b);
break;
case '-':
return Minus(a, b);
break;
case '*':
return Multiply(a, b);
break;
case '/':
return Divide(a, b);
break;
default:
std::cout << "The symbol you entered is not valid.";
};
return 0;
};
void TestCalculator() {
float a;
float b;
char Operator;
CalculatorOOP CalcOOP;
std::cout << "First number: ";
std::cin >> a;
std::cout << "Second number: ";
std::cin >> b ;
std::cout << "Choose desired operator (+,-,*,/): ";
std::cin >> Operator;
std::cout << CalcOOP.ResultSwitch(Operator, a, b);
int main() {
TestCalculator();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire