I'm a beginner in coding, in general, and as an academic student, I, along with my colleagues, started learning C++ first in our first year.
I was given an assignment to write a simple calculator using the aforementioned language. And while I could work through it using the 'switch' method, I still have a problem with my program:
#include <iostream>
using namespace std;
int main() {
char z;
double x,y,a,b,c,d;
cout << "Enter two operands, and an operator: ";
cin >> x >> y >> z;
a = x + y;
b = x-y;
c = x /y;
d = x * y;
switch (z) {
case '+':
cout << "The result is: " << a << endl;
break;
case '-':
cout << "The result is: " << b << endl;
break;
case '/':
cout << "The result is: " << c << endl;
break;
case '*':
cout << "The result is: " << d << endl;
break;
default:
cout << "Your operator is good! \n";
}
return 0;
}
In execution, after the first 'cout' statement appears, I tried entering an 'operator' or any other character; assigning it to the 'x' variable and expecting to get an error due to assigning a character to a 'double'-type variable (x). However, this didn't happen and doing so returned my switch's default statement instead.
Why did that happen?