190k views
5 votes
Assume that an int variable age has been declared and already given a value and assume that a char variable choice has been declared as well. Assume further that the user has just been presented with the following menu:

S: hangar steak, red potatoes, asparagus

T: whole trout, long rice, brussel sprouts

B: cheddar cheeseburger, steak fries, cole slaw

(Yes, this menu really IS a menu!)

Write some code that reads a single character (S or T or B) into choice . Then the code prints out a recommended accompanying drink as follows:

If the value of age is 21 or lower, the recommendation is "vegetable juice" for steak, "cranberry juice" for trout, and "soda" for the burger. Otherwise, the recommendations are "cabernet", "chardonnay", and "IPA" for steak, trout, and burger respectively. Regardless of the value of age , your code should print "invalid menu selection" if the character read into choice was not S or T or B.

2 Answers

4 votes

Final answer:

You need to write code that takes a single character input ('S', 'T', or 'B') for menu selection, checks the 'age' variable, and prints out an accompanying drink recommendation. If 'age' is 21 or below, recommend non-alcoholic drinks; otherwise, recommend alcoholic drinks. For any other input, print 'invalid menu selection'.

Step-by-step explanation:

To address the scenario, you will need to write code that handles input and conditional logic. Here's a simple outline in pseudocode:

get character input into choice
if choice == 'S'
if age <= 21
print 'vegetable juice'
else
print 'cabernet'
else if choice == 'T'
if age <= 21
print 'cranberry juice'
else
print 'chardonnay'
else if choice == 'B'
if age <= 21
print 'soda'
else
print 'IPA'
else
print 'invalid menu selection'

In your actual code, replace the pseudocode with syntax that corresponds to the programming language you are using. This code reads the choice character, checks the age variable, and prints the recommended accompanying drink based on these inputs. If the 'choice' character does not match any of the menu options, it outputs 'invalid menu selection'.

User Marin Leontenko
by
4.2k points
4 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

int age = 28;

char menu_selection;

cout << "Choose an item (S, T, or B) on the menu: ";

cin >> menu_selection;

if (menu_selection == 'S'){

if (age <= 21){

cout<<"Recommendation is vegetable juice";

}

else{

cout<<"Recommendation is cabernet";

}

}

else if (menu_selection == 'T'){

if (age <= 21){

cout<<"Recommendation is cranberry juice";

}

else{

cout<<"Recommendation is chardonnay";

}

}

else if (menu_selection == 'B'){

if (age <= 21){

cout<<"Recommendation is soda";

}

else{

cout<<"Recommendation is IPA";

}

}

else{

cout<<"Invalid menu selection";

}

return 0;

}

Step-by-step explanation:

Here is your solution in C++.

Initialize age, declare menu selection, and ask the user to make their choice.

Then check for each condition using if-else structure. Depending on the user choice and value of the age, print the recommendation.

User Little Bee
by
4.5k points