Hello ,
I need help understanding what is going on in the program below. I got this program from example 2 towards the bottom of the page from the following c++ tutorial: http://xoax.net/comp/cpp/console/Lesson27.php
If someone can explain in words exactly what is happening I will appreciate it. I understand switch statements however this is the first time that I have come across a switch statement that contains a case within another case. I dont understand why I end up with the results I get for the case that contains another case. For example, when the line in green is executed what happens that "Mystery" is not outputted but Novel and Fiction get outputted?
Here is the code:
Here is the program output:
I need help understanding what is going on in the program below. I got this program from example 2 towards the bottom of the page from the following c++ tutorial: http://xoax.net/comp/cpp/console/Lesson27.php
If someone can explain in words exactly what is happening I will appreciate it. I understand switch statements however this is the first time that I have come across a switch statement that contains a case within another case. I dont understand why I end up with the results I get for the case that contains another case. For example, when the line in green is executed what happens that "Mystery" is not outputted but Novel and Fiction get outputted?
Here is the code:
Rich (BB code):
#include <iostream>
enum EBookType { keMystery,
keNovel,
keFiction,
keNonFiction};
void PrintBookType( EBookType eType)
{
using namespace std;
switch (eType) {
case keMystery:
{
cout << "Mystery" << endl;
case keNovel:
{
cout << "Novel" << endl;
case keFiction:
{
cout << "Fiction" << endl;
break;
}
}
}
case keNonFiction:
{
cout << "Non-fiction" << endl;
break;
}
default:
{
cout << "unknown type" << endl;
break;
}
}
}
int main()
{
using namespace std;
EBookType eAtlasShrugged = keMystery;
EBookType eLionWitchWardrobe = keNovel;
EBookType eTheRepublic = keNonFiction;
PrintBookType(eAtlasShrugged);
cout << endl;
PrintBookType(eLionWitchWardrobe);
cout << endl;
PrintBookType(eTheRepublic);
cout << endl;
PrintBookType( (EBookType) 85 );
return EXIT_SUCCESS;
}
Rich (BB code):
Mystery
Novel
Fiction
Novel
Fiction
Non-fiction
unknown type
Press any key to continue . . .