noob question re: switch case & loops
So I'm brand new at this C++ business, having just spent some time learning over the past few days on my spare time. I'm trying to write a very simple menu program that will take your selection, input, and then output a simple mathematical calculation for you.
I'm running into problems while compiling (using MS Visual Studio 2008)..
Does anyone see what I'm doing wrong?
Code:
#include <iostream>
using namespace std;
int multiply(int x, int y );
int divide(int x, int y );
int subtract(int x, int y );
int main()
{
int l;
int x;
int y;
int input;
while ( input != 1, 2, 3, 4 ){
if ( l > 0 )
{
cout>>"You have pressed an invalid key, please choose one of the selections below\n";
}
cout<<"1. Multiply\n";
cout<<"2. Divide\n";
cout<<"3. Subtract\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> input;
switch ( input ) {
case 1: // Note the colon, not a semicolon
cout>>"please enter the first number to be multiplied" ;
cin<< x ;
cin.ignore();
cout>>"please enter the second number to be multiplied" ;
cin<< y ;
cin.ignore();
cout>>"the product of those two numbers is:">>multiply ( x ,y ) >>"\n";
break;
case 2: // Note the colon, not a semicolon
cout>>"please enter the first number to be divided" ;
cin<< x ;
cin.ignore();
cout>>"please enter the second number to be divided" ;
cin<< y ;
cin.ignore();
cout>>"the product of those two numbers is:">>divide ( x ,y ) >>"\n";
break;
case 3: // Note the colon, not a semicolon
cout>>"please enter the first number to be subtracted" ;
cin<< x ;
cin.ignore();
cout>>"please enter the second number to be subtracted" ;
cin<< y ;
cin.ignore();
cout>>"the product of those two numbers is:">>subtract ( x ,y ) >>"\n";
break;
case 4: // Note the colon, not a semicolon
cout<<"Thank you for playing!\n";
break;
}
l++;
endl;
}
cin.get();
}
int multiply(int x, int y )
{
return x * y;
}
int divide(int x, int y )
{
return x / y;
}
int subtract (int x, int y )
{
return x - y;
}
you guys are great! but check out this one
ok.. with a little help from you guys, I figured it out! It (almost) works as-designed. Not too shabby for day 2 of c++...
But here's the thing. If you choose to multiply a number, and you enter an 11 digit number, the program goes all wacky on me. Thoughts anyone?
p.s. I would have included the compiler errors from visual studio if there wasn't about 1000 lines of it. (Don't ask, I don't know why either)
Code:
#include <iostream>
using namespace std;
int multiply(int x, int y );
int divide(int x, int y );
int subtract(int x, int y );
int main()
{
int exit;
int x;
int y;
int input;
while (exit != 1)
{
cout<<"1. Multiply\n";
cout<<"2. Divide\n";
cout<<"3. Subtract\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> input;
cin.ignore();
if ( input > 4 )
{
cout<<"You have pressed an invalid key, please choose one of the selections below\n";
}
switch ( input )
{
case 1: // Note the colon, not a semicolon
cout<<"please enter the first number to be multiplied (or enter an 11 digit number to see something wacky) :\n" ;
cin>> x ;
cin.ignore();
cout<<"please enter the second number to be multiplied:\n" ;
cin>> y ;
cin.ignore();
cout<<"the product of those two numbers is:"<<multiply ( x ,y ) <<"\n";
break;
case 2: // Note the colon, not a semicolon
cout<<"please enter the first number to be divided:\n" ;
cin>> x ;
cin.ignore();
cout<<"please enter the second number to be divided:\n" ;
cin>> y ;
cin.ignore();
cout<<"the product of those two numbers is:"<<divide ( x ,y ) <<"\n";
break;
case 3: // Note the colon, not a semicolon
cout<<"please enter the first number to be subtracted:\n" ;
cin>> x ;
cin.ignore();
cout<<"please enter the second number to be subtracted:\n" ;
cin>> y ;
cin.ignore();
cout<<"the product of those two numbers is:"<<subtract ( x ,y ) <<"\n";
break;
case 4: // Note the colon, not a semicolon
cout<<"Are you sure you want to quit?\n";
cout<<"1. Exit\n";
cout<<"2. Keep playing with crappy math tool for some reason\n";
cout<<"Only the gods await your decision:";
cin>> exit;
break;
}
}
cin.get();
}
int multiply(int x, int y )
{
return x * y;
}
int divide(int x, int y )
{
return x / y;
}
int subtract (int x, int y )
{
return x - y;
}