Hi, how the sytem read the input from my computer as 1, 2, 3, or 4? is that read from the case name? eg: case 1, then if i input 1, it will read from case "1"? Thanks...
#include <iostream>
#include <conio>
int main ()
{
int choice;
cout<<"What flavour ice cream do you want?"<<endl;
cout<<"Enter 1 for chocolate"<<endl;
cout<<"Enter 2 for vanilla"<<endl;
cout<<"Enter 3 for strawberry"<<endl;
cout<<"Enter 4 for Yam"<<endl;
cout<<"Enter your choice: ";
cin>> choice;
switch (choice)
{
case 1:
cout<<"Chocolate, Muhammad's favourite."<<endl;
break;
case 2:
cout<<"Vanilla, Ismael's favourite"<<endl;
break;
case 3:
cout<<"Strawberry, Adibah's favourite"<<endl;
break;
case 4:
cout<<"Yam, Munirah's favourite"<<endl;
break;
default:
cout<<"We don't have any"<<endl;
cout<<"Make another selection"<<endl;
}
getch ();
return 0;
}
Thanks, just now the switch case can read my keyboard input, 1, 2, .... so how if i want to have a input like a, b, c? can it happen too if let say, i press "a" on my keyboard, then it will run the 1st case and give output like "Chocolate, Muhammad's favourite."? Thanks...
Eh? Maybe I'm misreading things, but you can most certainly switch characters. In fact, you could probably take the exact copy paste from FlashDrive's "You CAN NOT use something like this:" code and it would work instantly.
I was hoping FlashDrive would explain why he thought it wouldn't work so that we could explain to him why it would, but eh. Switching on constants for POD types works fine. Switching on variables doesn't.
EDIT: For those who don't know, POD means Plain Old Data. It basically refers to non-compound C variable types.
Gaminic, u r right, it works on character using switch but i don't know why need this 'x'; inside char c='x';, can I just declare to "0" since it can works too like the one below...
@atjm88
The initialization step is trivial, because 'choice' is overwritten by cin on line 14.
Initialization is only important if the value is going to be used before an assignment takes place. Take this example:
1 2 3
int i;
i += 20;
cout << i;
What's the value of i? Who knows. Line 1 initializes i without assigning a value, line 2 adds 20 to i. "empty" initialization means i could have any value (some compilers set it to 0 automatically, but that's not guaranteed behavior).
Unless you're certain 'i' will be assigned a value before it is used, you need to initialize it with a (neutral) value:
Question here:
Can I declare my char to zero? Is that a correct way char choice = 0;? Or I can only declare integer to zero int choice = 0;?
another question is
how about this kind of code char choice = 'a';? Is that means I declare choice to alphabet a? Or I can write in this way too char choice = a;? Thanks^^
Question here:
Can I declare my char to zero? Is that a correct way char choice = 0;? Or I can only declare integer to zero int choice = 0;?
Yes, you can initialise your char to 0. It's quite safe, as the ASCII value for 0 is NULL.
another question is
how about this kind of code char choice = 'a';? Is that means I declare choice to alphabet a? Or I can write in this way too char choice = a;? Thanks^^
The first one is fine. The second one will give you an error unless a is a previously defined variable.
char's are basically just 1byte numbers. The compiler (or something else, I don't really know, but it doesn't matter) knows to 'translate' it via an ASCII table when it's shown to the user, but internally it's just a number. Like iHutch105 said, 'a' is actually 97. This letter<->number relationship is further exploited by seeing that 'b' = 'a'+1 and 'z' = 'a'+25.
That being said, while 'a' is 97, a isn't. a is a variable, because it has no quotes. If a doesn't exist (or is a number that can't be translated into a char), the compiler will show errors.
So means that char choice = 'a'; = char choice = 97;? Am I right?
How about char choice = a;? can works? or need to declare char a = 97 then only can use this code char choice = a;? which is actually similar to char choice = 'a';? Is that correct? TQ...
I tried to check what's the number of "choice" actually and 1st expectation is "97", but then after run, it gives answer "a" instead of "97" (Line 6-9), may I know why? TQ...