I was playing with my code I wrote and I'm curious why wouldn't this work? I also want make sure the code I just wrote is what the problem creator wanted. From here http://www.cplusplus.com/articles/N6vU7k9E/ it's called Cola Machine, I first had it with if and else if and then I modify it to using switch statement.
You are trying to nest more control flow than what you need.
switch(x) will look at the value of x and jump to a matching case and execute statements until a break statement or the closing bracket of the switch is encountered
In your second code, if choice is 2, the switch statement will jump to case 2 and execute the cout statement in the default case (this is because there is no break stopping it from doing so), and then the switch will be done because it runs into the closing bracket of the switch.
The lines 18 through 39 are only reachable if choice is 1.
You only need one or the other in this situation, which is why I said you are nesting more than you need. Both a switch and an if-else if-else are capable of looking at one digit number and then splitting one of 6 different directions.
I'm assuming the correct code is wrong as well? Just out of curiousity how would you approach this when coding it. It make sense what you are stating though, thank you :)
// Create a lookup table:
string bevnames[5] = {
"CocaCola",
"Sprite",
"Orange Soda",
"Mountain Dew",
"Pepsi"
};
// Get the choice:
int choice;
cout << " Coke Cola press 1, Sprite press 2, Orange soda press 3, Mountain Dew press 4, Pepsi press 5." << endl;
cout << "Select the soda you would like from the available menu." << endl;
cin >> choice;
// use the choice to index the lookup table:
if(choice < 1 || choice > 5) // out of bounds?
{
cout << "Error. Choice was not valid. Here is your money back." << endl;
}
else // valid choice
{
cout << "The soda you have selected is " << bevnames[ choice-1 ] << endl;
}
In this case I wouldn't be able to use an array. So how would I approach it just using Switch and if statement and not arrays. For a beginner that does not know much about coding. I know how to code I know what arrays are I just simply forgot everything because I haven't program in 2 months. So, I'm trying to re-learn from the very beginning. By solving the problems I found in the article section on here.
Although it is nice to see another advance option once I re learned arrays this could be very useful, so thank you :)
The golden rule for programming is "use the right tool for the right job". If you are trying to learn how to use switch statements effectively, this is not a good problem for that, since switch statements are not the right tool in this case.
So how would I approach it just using Switch and if statement and not arrays.
Well for starters it would be switch OR if statements -- you would not use both. Both are types of control statements, and both are very similar.
If you were to twist my arm and say I couldn't use arrays for this problem, I'd probably do one of these:
I'm confuse if( selection.empty() ) // invalid <------- is this a function it looks like you're calling it inside your if statement. I could be wrong though. Also that's a good rule to follow by.
I'm tempted to ask "why not?" Because in the directions here it tells us what it wants. http://www.cplusplus.com/articles/N6vU7k9E/
I really do appreciate your sources because it gives me a better out look on different ways to approach this.
You can also have integers evaluate to booleans inside an if clause. Generally I do not do this, because I believe it is more cryptic. I just wanted to point this out because a lot of beginners accidentally use assignment (=) instead of equality check (==)
Checkout this article here: http://www.cplusplus.com/forum/articles/3483/
#include <iostream>
usingnamespace std;
int main() {
string bev1 = "Coke Cola";
string bev2 = "Sprite";
string bev3 = "Orange Soda";
string bev4 = "Mountain Dew";
string bev5 = "Pepsi";
int choice;
cout
<< " Coke Cola press 1, Sprite press 2, Orange soda press 3, Mountain Dew press 4, Pepsi press 5."
<< endl;
cout << "Select the soda you would like from the available menu." << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "The soda you have selected is" << " " << bev1 << " enjoy! "
<< endl;
break;
case 2:
cout << "The soda you have selected is" << " " << bev2 << " enjoy! "
<< endl;
break;
case 3:
cout << "The soda you have selected is" << " " << bev3 << " enjoy! "
<< endl;
break;
case 4:
cout << "The soda you have selected is" << " " << bev4 << " enjoy! "
<< endl;
break;
case 5:
cout << "The soda you have selected is" << " " << bev5 << " enjoy! "
<< endl;
break;
default:
cout << "Error. Choice was not valid, here is your money back." << endl;
}
return 0;
}
EDIT: So I've played with the new code and I switch the cases number around and came to a realization that cases actually correspond with the choices. Correct if I am wrong because I want to learn. Am I right? It's probably vague what I just said here, but worth a shot to see your other view on this.
Okay here's my new source let me know if this is correct,
I don't see any glaring problems.
So I've played with the new code and I switch the cases number around and came to a realization that cases actually correspond with the choices. Correct if I am wrong because I want to learn. Am I right?
Yes. That is what switch does. You give switch a value to examine, and it jumps to the appropriate 'case' label depending on the contents of that value.
Since you are doing switch(choice) ... the value being examined is 'choice'. Therefore if choice==1, it will jump to the case 1: label.
#include <iostream>
usingnamespace std;
int main()
{
string bev1 = "Coke Cola";
string bev2 = "Sprite";
string bev3 = "Orange Soda";
string bev4 = "Mountain Dew";
string bev5 = "Pepsi";
int choice;
cout<<"\n1.Coke Cola\n2.Sprite\n3.Orange soda\n4.Mountain Dew\n5.Pepsi"<< endl;
cout << "Select the soda you would like from the available menu." << endl;
cin >> choice;
cout << "The soda you have selected is :\t";
switch (choice)
{
case 1:
cout<<bev1;
case 2:
cout<<bev2;
case 3:
cout<<bev3;
case 4:
cout<<bev4;
case 5:
cout<<bev5;
}
cout<<"\t Enjoy !!!";
return 0;
}