Chapter 4

Enumerated Data Type

posted Jan 29, 2013, 12:27 AM by Karwan Jacksi   [ updated Jan 29, 2013, 12:27 AM ]

// This program demonstrates an enumerated data type.
#include <iostream>
using namespace std;
// Declare the enumerated type
enum Roster { Tom = 1, Sharon, Bill, Teresa, John };
// Sharon – John will be assigned default values 2-5.

int main()
{
 int who;

 cout << "This program will give you a student's birthday.\n";
 cout << "Whose birthday do you want to know?\n";
 cout << "1 = Tom\n";
 cout << "2 = Sharon\n";
 cout << "3 = Bill\n";
 cout << "4 = Teresa\n";
 cout << "5 = John\n";
 cin >> who;

 switch (who)
 {
case Tom : cout << "\nTom's birthday is January 3.\n";
break;
case Sharon: cout << "\nSharon's birthday is April 22.\n";
break;
case Bill : cout << "\nBill's birthday is December 19.\n";
break;
case Teresa: cout << "\nTeresa's birthday is February 2.\n";
break;
case John : cout << "\nJohn's birthday is June 17.\n";
break;
default : cout << "\nInvalid selection\n";
 }
 return 0;
 }

Switch- How to benefit from the remaining cases

posted Jan 28, 2013, 2:46 AM by Karwan Jacksi   [ updated Jan 28, 2013, 2:46 AM ]

#include <iostream>

using namespace std;


void main()
{
int modelNum;
 // Display available models and get the user's choice
 cout << "Our TVs come in three models:\n";
 cout << "The 100, 200, and 300. Which do you want? ";
 cin >> modelNum;

 // Display the features of the selected model
 cout << "That model has the following features:\n";
 switch (modelNum)
 {
 case 300: cout << " Picture-in-a-picture\n";
 case 200: cout << " Stereo sound\n";
 case 100: cout << " Remote control\n";
 break;
 default : cout << "You can only choose the 100, 200, or 300.\n ";
 }

system("pause");
}

Switch with break

posted Jan 28, 2013, 2:20 AM by Karwan Jacksi   [ updated Jan 28, 2013, 2:21 AM ]

// This program demonstrates the use of a switch statement.
// The program simply tells the user what character they entered.
#include <iostream>
using namespace std;
int main()
{
char choice;
cout << "Enter A, B, or C: ";
cin >> choice;

switch (choice)
{
case 'A':cout << "You entered A.\n";
break;
case 'B':cout << "You entered B.\n";
break;
case 'C':cout << "You entered C.\n";
break;
default: cout << "You did not enter A, B, or C!\n";
}
return 0;
}

Switch with no break

posted Jan 28, 2013, 1:53 AM by Karwan Jacksi   [ updated Jan 28, 2013, 1:53 AM ]

#include <iostream>

using namespace std;

void main()
{
// This program demonstrates how a switch statement
// works if there are no break statements.
char choice;
cout << "Enter A, B, or C: ";
cin >> choice;
// The following switch statement is missing its break statements!
switch (choice)
 {
case 'A':cout << "You entered A.\n";
case 'B':cout << "You entered B.\n";
case 'C':cout << "You entered C.\n";
default :cout << "You did not enter A, B, or C!\n";
 }

 system("pause");
}

1-4 of 4