Why wouldn't this work? Just curious!

closed account (yR9wb7Xj)
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.
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
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace 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:
	if(choice == 1){
		cout << "The soda you have selected is" << " " << bev1 << " enjoy! " << endl;
	}
	break;
	case 2:
	if(choice ==2){
		cout << "The soda you have selected is" << " " << bev2 << " enjoy! " << endl;
	}
	break;
	case 3:
	if(choice == 3){
		cout << "The soda you have selected is" << " " << bev3 << " enjoy! " << endl;
	}
	break;
	case 4:

	if(choice ==4){
			cout << "The soda you have selected is" << " " << bev4 << " enjoy! " << endl;
	}
	break;
	case 5:


	 if(choice ==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;
}


------Why wouldnt this work-------
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
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace 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:
	if(choice == 1){
		cout << "The soda you have selected is" << " " << bev1 << " enjoy! " << endl;
	}
		else if(choice ==2){
				cout << "The soda you have selected is" << " " << bev2 << " enjoy! " << endl;
		}

			else if(choice == 3){
				cout << "The soda you have selected is" << " " << bev3 << " enjoy! " << endl;
			}


			else if(choice ==4){
					cout << "The soda you have selected is" << " " << bev4 << " enjoy! " << endl;
			}



			else if(choice ==5){
			cout << "The soda you have selected is" << " " << bev5<< " enjoy! " << endl;
			 }
	break;
	case 2:



default:
	cout << "Error. Choice was not valid, here is your money back." << endl;
	}

	return 0;
}
Last edited on
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.
Last edited on
closed account (yR9wb7Xj)
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 :)
Lookup tables are almost always better than jump tables.

You're effectively printing the same thing for each choice -- so there's no need to change logic/code. Only the data you're printing needs to change:

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
// 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;
}
Last edited on
closed account (yR9wb7Xj)
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 :)
In this case I wouldn't be able to use an array.


I'm tempted to ask "why not?"

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:

else if chain:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string selection;

if     (choice == 1)    selection = "CocaCola";
else if(choice == 2)    selection = "Sprite";
else if(choice == 3)    selection = "Orange Soda";
else if(choice == 4)    selection = "Mountain Dew";
else if(choice == 5)    selection = "Pepsi";

if( selection.empty() ) // invalid
{
    cout << "Invalid selection";
}
else
{
    cout << "You have selected " << selection;
}


Switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string selection;

switch(choice)
{
    case 1:     selection = "CocaCola";         break;
    case 2:     selection = "Sprite";           break;
    case 3:     selection = "Orange Soda";      break;
    case 4:     selection = "Mountain Dew";     break;
    case 5:     selection = "Pepsi";            break;
}

if( selection.empty() ) // invalid
{
    cout << "Invalid selection";
}
else
{
    cout << "You have selected " << selection;
}
closed account (yR9wb7Xj)
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.
Last edited on
is this a function it looks like you're calling it inside your if statement.


Yes and yes.

empty() is a function available in the string class. It returns a bool (true of false) to indicate whether or not the string is empty.

By putting it in an if statement, we are examining the return value.

A more verbose way to do that would be:
1
2
bool wasempty = selection.empty();
if( wasempty )  // invalid 



Because in the directions here it tells us what I need to use for it.


Fair enough.
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/
closed account (yR9wb7Xj)
Okay here's my new source let me know if this is correct, thanks guys. Thanks Kevinkjt2000 I will check that out :)
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
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace 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.
Last edited on
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.
closed account (yR9wb7Xj)
Thank you again! I fully understand it now. :)
//you could also do it this way :

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
#include <iostream>

using namespace 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;
}
Last edited on
@TheCplusplusMan: You forgot break statements
thanks man @kevinkjt2000,

Put

break;

between cases so as to not let the control flow
add those too @Codex23jv
Last edited on
Topic archived. No new replies allowed.