In: Computer Science
/* Homework Lab 8
Nested If statements
Write the nested decision logic to print the recommended
meal
based on the user's choices for Restaurant and Load given the
table
shown below:
Restaurant Preference Meal
-----------------------------------------------------
(M)cDonald's
(B)eef Quarter Pounder
(C)hicken
Chicken McNuggets
(F)ish
Filet-o-Fish
(S)alad Side
Salad
(O)utback
(B)eef
Ribeye
(C)hicken Alice
Springs Chicken
(F)ish Grilled
Tilapia
(S)alad Aussie
Cobb Salad
(G)ibsons
(B)eef Bone-in
Filet Mignon
(C)hicken Whole
Spit-Roasted Chicken
(F)ish Grilled
Norwegian Salmon
(S)alad Alaskan
King Crab Salad
For example, if the user chooses '(M)cDonald's' and
'(B)eef'
then the appropriate response should be "Quarter Pounder"
If the user makes any selection other than B, b, C, c, F,
f,
S, or s then the approprate response should be:
"You entered an invalid meal preference selection."
*/
#include<iostream>
#include<cctype> // for toupper()
using namespace std;
int main()
{
// Declare program variables for user input.
char restaurantChoice;
char mealPreference;
// Prompt and get user's choice for
restaurant.
cout << "Enter your choice for Restaurant"
<< endl
<< "(M)cDonald's, (O)utback,
or (G)ibsons: ";
cin >> restaurantChoice;
// Ensure restaurantChoice is uppercase
restaurantChoice = toupper(restaurantChoice);
// Prompt and get user's choice for meal
preference.
cout << "\nEnter your meal preference:" <<
endl
<< "(B)eef, (C)hicken,
(F)ish, (S)alad: ";
cin >> mealPreference;
// Ensure mealPreference is uppercase
mealPreference = toupper(mealPreference);
cout << endl;
// For each chosen Restaurant,
// output an appropriate response based on Meal
Preference.
if (restaurantChoice == 'M') // McDonald's
{
cout << "You chose
McDonald's." << endl;
// TODO: Write an extened
if-else-if based on the user's choice
// for Meal Preference to print one
of the following messages:
//
// B = Quarter Pounder
// C = Chicken McNuggets
// F = Filet -o- Fish
// S = Side Salad
// Otherwise = You entered an
invalid meal preference selection.
cout << endl;
}
else if (restaurantChoice == 'O') // Outback
{
cout << "You chose Outback."
<< endl;
// TODO: Write an extened
if-else-if based on the user's choice
// for Meal Preference to print one
of the following messages:
//
// B = Ribeye
// C = Alice Springs Chicken
// F = Grilled Tilapia
// S = Aussie Cobb Salad
// Otherwise = You entered an
invalid meal preference selection.
cout << endl;
}
else if (restaurantChoice == 'G') // Gibsons
{
cout << "You chose Gibsons."
<< endl;
// TODO: Write an extened
if-else-if based on the user's choice
// for Meal Preference to print one
of the following messages:
//
// B = Bone-in Filet Mignon
// C = Whole Spit-Roasted
Chicken
// F = Grilled Norwegian
Salmon
// S = Alaskan King Crab
Salad
// Otherwise = You entered an
invalid meal preference selection.
cout << endl;
}
else // Invalid Restaurant selection
cout << "You entered an
invalid restaurant selection: "
<< restaurantChoice <<
endl;
cout << "\nEnd Program" << endl;
return 0;
}
/*
Sample Program Output
-----------------------------------------------------------
Test #1
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: X
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: B
You entered an invalid restaurant selection: X
End Program
Press any key to continue . . .
Test #2
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: X
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: X
You entered an invalid restaurant selection: X
End Program
Press any key to continue . . .
Test #3
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: M
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: B
You chose McDonald's.
We recommend Quarter Pounder.
End Program
Press any key to continue . . .
Test #4
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: m
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: c
You chose McDonald's.
We recommend Chicken McNuggets.
End Program
Press any key to continue . . .
Test #5
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: M
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: F
You chose McDonald's.
We recommend Filet-o-Fish.
End Program
Press any key to continue . . .
Test #6
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: m
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: S
You chose McDonald's.
We recommend Side Salad.
End Program
Press any key to continue . . .
Test #7
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: M
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: X
You chose McDonald's.
You entered an invalid meal selection.
End Program
Press any key to continue . . .
Test #8
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: O
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: B
You chose Outback.
We recommend Ribeye.
End Program
Press any key to continue . . .
Test #9
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: o
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: c
You chose Outback.
We recommend Alice Springs Chicken.
End Program
Press any key to continue . . .
Test #10
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: O
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: F
You chose Outback.
We recommend Grilled Tilapia.
End Program
Press any key to continue . . .
Test #11
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: o
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: s
You chose Outback.
We recommend Aussie Cobb Salad.
End Program
Press any key to continue . . .
Test #12
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: o
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: x
You chose Outback.
You entered an invalid meal selection.
End Program
Press any key to continue . . .
Test #13
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: G
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: b
You chose Gibsons.
We recommend a Bone-in Filet Mignon.
End Program
Press any key to continue . . .
Test #14
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: G
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: C
You chose Gibsons.
We recommend Whole Spit-Roasted Chicken.
End Program
Press any key to continue . . .
Test #15
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: g
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: f
You chose Gibsons.
We recommend Grilled Norwegian Salmon.
End Program
Press any key to continue . . .
Test #16
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: g
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: S
You chose Gibsons.
We recommend Alaskan King Crab Salad.
End Program
Press any key to continue . . .
Test #17
================================
Enter your choice for Restaurant
(M)cDonald's, (O)utback, or (G)ibsons: G
Enter your meal preference:
(B)eef, (C)hicken, (F)ish, (S)alad: X
You chose Gibsons.
You entered an invalid meal selection.
End Program
Press any key to continue . . .
*/
CODE
#include<iostream>
#include<cctype> // for toupper()
using namespace std;
int main()
{
// Declare program variables for user input.
char restaurantChoice;
char mealPreference;
// Prompt and get user's choice for restaurant.
cout << "Enter your choice for Restaurant" << endl
<< "(M)cDonald's, (O)utback, or (G)ibsons: ";
cin >> restaurantChoice;
// Ensure restaurantChoice is uppercase
restaurantChoice = toupper(restaurantChoice);
// Prompt and get user's choice for meal preference.
cout << "\nEnter your meal preference:" << endl
<< "(B)eef, (C)hicken, (F)ish, (S)alad: ";
cin >> mealPreference;
// Ensure mealPreference is uppercase
mealPreference = toupper(mealPreference);
cout << endl;
// For each chosen Restaurant,
// output an appropriate response based on Meal Preference.
if (restaurantChoice == 'M') // McDonald's
{
cout << "You chose McDonald's." << endl;
if (mealPreference == 'B' || mealPreference == 'b' ) {
cout << "We recommend Quarter Pounder." << endl;
} else if (mealPreference == 'C' || mealPreference == 'c' ) {
cout << "We recommend Chicken McNuggets." << endl;
} else if (mealPreference == 'F' || mealPreference == 'f' ) {
cout << "We recommend Filet-o-Fish." << endl;
} else if (mealPreference == 'S' || mealPreference == 's' ) {
cout << "We recommend Side Salad." << endl;
} else {
cout << "You entered an invalid meal selection." << endl;
}
cout << endl;
}
else if (restaurantChoice == 'O') // Outback
{
cout << "You chose Outback." << endl;
if (mealPreference == 'B' || mealPreference == 'b' ) {
cout << "We recommend Ribeye." << endl;
} else if (mealPreference == 'C' || mealPreference == 'c' ) {
cout << "We recommend Alice Springs Chicken." << endl;
} else if (mealPreference == 'F' || mealPreference == 'f' ) {
cout << "We recommend Grilled Tilapia." << endl;
} else if (mealPreference == 'S' || mealPreference == 's' ) {
cout << "We recommend Aussie Cobb Salad." << endl;
} else {
cout << "You entered an invalid meal selection." << endl;
}
cout << endl;
}
else if (restaurantChoice == 'G') // Gibsons
{
cout << "You chose Gibsons." << endl;
if (mealPreference == 'B' || mealPreference == 'b' ) {
cout << "We recommend Bone-in Filet Mignon." << endl;
} else if (mealPreference == 'C' || mealPreference == 'c' ) {
cout << "We recommend Whole Spit-Roasted Chicken." << endl;
} else if (mealPreference == 'F' || mealPreference == 'f' ) {
cout << "We recommend Grilled Norwegian Salmon." << endl;
} else if (mealPreference == 'S' || mealPreference == 's' ) {
cout << "We recommend Alaskan King Crab Salad." << endl;
} else {
cout << "You entered an invalid meal selection." << endl;
}
cout << endl;
}
else // Invalid Restaurant selection
cout << "You entered an invalid restaurant selection: "
<< restaurantChoice << endl;
cout << "\nEnd Program" << endl;
return 0;
}