first add prompt to user Second What happens if I delete the "break" Third why my code is not working when my "b" is ==0?

Viewed 41
int main() {
    int a, b, res; char op; bool f = true;
std::cin >> a >> op >> b; // add prompt to user
switch (op) {
case '+': res = a + b; break; // if 'break' is missed?
case '-': res = a - b; break;
case '*': res = a * b; break;
case '/': res = a / b; break; // if b is zero?
default: f = false;
}
if (f) std::cout << a << op << b << "=" << res << std::endl;
else std::cout << "Unknown operation: " << op << std::endl;
}**

First, add prompt to user. Second what happens if I delete the "break"? Third, why my code is not working when my "b" is ==0?

1 Answers
  1. If you remove "break" then compiler goes on to the next "case" there is an info on switch
  2. It doesn't work with b == 0 because you can't divide by zero ^-^
Related