Hello all,
New to the forum and new to C++... Sorry if I sound like I'm a noob but I am... I just started playing with C++ yesterday and am following the tutorials on this site. I'm having a little problem with the default part in the switch. It doesn't seem to work and just makes an infinite loop. Any help would be appreciated! Also any input on making the code more efficient or any input at all would be very helpful! Thanks!
Here's my code:
It's an improvise of the code on http://www.cprogramming.com/tutorial/lesson5.html. Don't know what the problem is. =/Code:#include <iostream> using namespace std; int x; int y; int ans; char cont; int add (); int subtract (); int multiply (); int divide (); int main() { do { int input; cout<<"1. Add two numbers\n"; cout<<"2. Subtract two numbers\n"; cout<<"3. Multiply two numbers\n"; cout<<"4. Divide two numbers\n"; cout<<"Selection: "; cin>> input; switch ( input ) { case 1: add (); break; case 2: subtract (); break; case 3: multiply (); break; case 4: divide (); break; default: cout<<"Error, bad input\n"; break; } cout<<"Your answer is : "<< ans <<"\n"; cin.get(); cout<<"Would you like to continue? (y/n)\n"; cin>> cont; cin.ignore(); } while ( cont != 'n' ); } int add () { cout<<"Please enter 2 numbers: \n"; cin>> x >> y; ans = x + y; cin.get(); } int subtract () { cout<<"Please enter 2 numbers: \n"; cin>> x >> y; ans = x - y; cin.get(); } int multiply () { cout<<"Please enter 2 numbers: \n"; cin>> x >> y; ans = x * y; cin.get(); } int divide () { cout<<"Please enter 2 numbers: \n"; cin>> x >> y; ans = x / y; cin.get(); }