In: Computer Science
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:
(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.
Please answer in C code
Thanks for the question.
Here is the completed code for this problem.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer.
Thanks!
===========================================================================
#include<stdio.h>
int main(){
int age=10; // you can change the age here
char choice;
printf("Enter your choice (S or T or B)? ");
scanf("%c",&choice);
// first validate the choice are either S or T or
B
if(choice=='S' || choice =='T' || choice =='B'){
// once the choice are correct
check the age is within 21
if(age<21) {
if( choice=='S')
printf("You should go for Vegetable Juice.\n");
else
if(choice=='T') printf("You should go for Cranberry
Juice.\n");
else
if(choice=='B') printf("You should go for Soda.\n");
}
// choice are either S or T or B
but age is greater than 21
else{
if( choice=='S')
printf("You should go for cabernet.\n");
else
if(choice=='T') printf("You should go for chardonnay.\n");
else
if(choice=='B') printf("You should go for IPA.\n");
}
}
else{
// when choice is neither S or T or
B
printf("Invalid menu
selection.\n");
}
}
=========================================================================