Sep 5, 2012 at 1:57am UTC
It means that the variable has no value. You need to assign it a value before you can do a check on it.
Sep 5, 2012 at 2:05am UTC
What is that?
1 2
switch (choice)
case '4'
Choice is an integer, so comparing it to a character literal isn't going to work as expected.
int choice;
You never assigned a value to
choice so the compiler just gave it a 'garbage' value.
Fixed Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int num, num2;
int choice;
cin >> choice;
switch (choice)
{
case (1):cout << " Please enter your numbers to add" << endl;
cin >> num >> num2;
cout << "num + num2 = " << num + num2 << endl;
break ;
case (2):cout << " Please enter your numbers to subtract" << endl;
cin >> num >> num2;
cout << "num - num2 =" << num - num2 << endl;
break ;
case (3):cout << " Please enter your numbers to multipy" << endl;
cin >> num >> num2;
cout << "num * num2 = " << num * num2 << endl;
break ;
case (4):cout << " Please enter your numbers to divied" << endl;
cin >> num >> num2;
cout << " num / num2 =" << num / num2 << endl << endl;
break ;
default :cout << " That is an invalid input" << endl;
}
return 0;
}
Last edited on Sep 5, 2012 at 2:09am UTC
Sep 5, 2012 at 10:23pm UTC
ok I understand now, I did not give the user the place to insert the action. Thanks guys. Just out of courisoity, would it be easier to use the if then statement with the do while instead of the switch?
Sep 5, 2012 at 10:49pm UTC
You can use either if-else or switch. I would prefer to use switch when the condition is a comparision with a fixed set of integral constants.
Last edited on Sep 5, 2012 at 10:50pm UTC
Sep 6, 2012 at 3:37am UTC
yes, i think its up to you. if-else or switch-case functions the same..