Hey guys, seeing as I've only been programming in C++ for a few weeks, I've looked around the forum and seen a brilliant thread with some beginner exercises. I've made a simple vending machine program that asks the user to key in an option of 1-5 for what drink they would like. Once the user has keyed in an option, they are then shown what option they have made. I made it using IF statements, and it worked perfectly. However, I'm having some trouble making the same program but with SWITCH statements. Can you guys have a look and tell me where I'm going wrong? (I'm using Anjuta IDE on Ubuntu 12.04 if you needed to know!)
Remove the ' ' from the nums. With those there, they are chars, not ints.
On another note:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
switch (beverage){
case 1:
cout << "You have chosen Coca Cola "; break;
case 2:
cout << "You have chosen Diet Coke "; break;
case 3:
cout << "You have chosen Sprite "; break;
case 4:
cout << "You have chosen Fanta Orange "; break;
case 5:
cout << "You have chosen water "; break;
}
Would probably be a better approach to indentation. Imagine you have 20 cases instead of just 5.
Edit: Or you can change the type of beverage to char instead of int.
beverage is a variable of type 'int', then the statement "case 'n' is wrong because of the embedded quotes ('n' denotes a variable of type char.) Replace 'n' with n: