C++ Calculator Exits -
i have problem first program in c++.i wrote calculator reason when type operation character quits.it doesn't show error or something,it exits. code visual c++
#include <iostream> using namespace std; int main() { float n1; float n2; float n3; int op; cout << "welcome calculator" << endl; cout << "type first number: "; cin >> n1; cout << "type second number: "; cin >> n2; cout << "type number operation" << endl; cout << "1 = addition" << endl; cout << "2 = subvision" << endl; cout << "3 = multiply" << endl; cout << "4 = division" << endl; cin >> op; if(op == 1) { n3 = n1 + n2; cout << "the result " << n3 << endl; } if(op == 2) { n3 = n1 - n2; cout << "the result " << n3 << endl; } if(op == 3) { n3 = n1 * n2; cout << "the result " << n3 << endl; } if(op == 4) { n3 = n1 / n2; cout << "the result " << n3 << endl; } return 0; }
you might want @ switch statement instead of multiple ifs. default statement can catch happening when none of expected cases match.
switch (op) { case 1: { // add break; } // other cases default { // unexpected, print error } }
Comments
Post a Comment