Switch statement help

So I am new to C++ (obvious from the forum I'm posting in), and my teacher gave us an assignment this week regarding switch statements. I have a basic understanding of them, but I can't fully grasp the idea. She is wanting us to write a program where the user inputs a code between 1, 2 and 3. If the code is not one of those three numbers, an error message is displayed. However, if the code is correct, then the output supplies a list of countries that "are included in the trip package". She advises using a switch statement to display the countries included in the trip package, and that some cases will not have a break afterward. I have what I believe to be the gist of the code put together, but I feel like I am doing it incorrectly for the assignment. She included a note at the end of the description that states:

"For codes 1, 2 and 3 'France, Italy' is displayed. For codes 1 and 2 'Germany' is displayed. Thus proper placement of the statement:
cout << "France, Italy"; in the switch statement will result in the statement appearing only once in the switch statement."

The trip codes are as follows:

Trip Code (Countries)

1 (England, Netherlands, Germany, France, Italy)
2 (Germany, France, Italy)
3 (France, Italy)



Could someone review my code thus far and advise if I am misunderstanding how to accomplish this assignment?


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

using std::cin;
using std::cout;
using std::endl;
using namespace std;

int main()
{
	char choice;

	cout << "Select from the following options for your European Trip Package\n"
		<< "\tCode\t1\n"
		<< "\tCode\t2\n"
		<< "\tCode\t3\n";
	cin >> choice;
	switch (choice)
	{
	case '1': cout << "England, Netherlands" << endl;

	case '2': cout << "Germany" << endl;

	case '3': cout << "France, Italy" << endl;
		break;
	default: cout << "Error. Invalid Choice" << endl;
		break;
	}

	system("pause");
	return 0;
}
Last edited on
"For codes 1, 2 and 3 'France, Italy' is displayed. For codes 1 and 2 'Germany' is displayed.

I'm not quite sure what the expected output is supposed to be -- you later defined Trip choices that seem to conflict with that.

Usually every case of a switch statement has a break; in it, and not just the last one. If you don't have a break; then you risk that something can "fall through". For example, your current code, upon hitting "1" , will show all three cases (everything)
that's the idea, for the code to «fall through»
not a particular fan of that use, though. too error prone.

@OP, your code seems fine.
Ah, ok, I misunderstood that for codes 1 and 2, 'Germany' is supposed to appear in addition to others. Upon re-reading, yeah, it looks like an intentional fall-through design.

Then all that's left is to touch-up the commas and newlines. Example in a do...while in an online compiler: https://repl.it/repls/ImpressiveFinancialRuntimeerror
Last edited on
@icy1, Thank you for the evaluation of my code. I honestly was just as confused when looking at the instructors directions. It wasn't until ne555 pointed out that it is supposed to "fall through" that it really made sense. I was reading it wrong I think.
Topic archived. No new replies allowed.