Clear Filters
Clear Filters

How do you make a certain selection from a user with the menu command display through switch,case and if,elseit,else statements?

2 views (last 30 days)
Hello! I am practicing material for my class and decided to make a restaraunt program where you are welcomed to the restaraunt, asked which drink you want, and whichever drink you select the program should display to the user 'drink ' coming right up!' but it does not do this. I am tyrying to do this both for user drink and food selection. I am also asking the user if they would like to continue/have dessert, and am using both switch case and if,elseif,else statements. It just needs some tidying up that I cannot figure out. Thank you in advance for the help!
clc
clear
close all
disp('Hello, welcome to Julize''s Restaraunt! Please take a seat.')
pause
drinks=menu('Please select which drink you would like to get started!','Lemonade','Coke','Water','Tea','Sprite');
switch drinks
case 'Lemonade'
disp([drinks ' coming right up!'])
drinks=1;
case 'Coke'
disp([drinks ' coming right up!'])
drinks=2;
case 'Water'
disp([drinks ' coming right up!'])
drinks=3;
case 'Tea'
disp([drinks ' coming right up!'])
drinks=4;
case 'Sprite'
disp([drinks ' coming right up!'])
drinks=5;
end
pause
food=menu('What would you like to eat today?','Steak & Fries','Fish & Rice','Hamburger & Fries','Shrimp & Rice');
switch food
case 'Steak & Fries'
disp([food ' coming right up!'])
food=1;
case 'Fish & Rice'
disp([food ' coming right up!'])
food=2;
case 'Hamburger & Fries'
disp([food ' coming right up!'])
food=3;
case 'Shrimp & Rice'
disp([food ' coming right up!'])
food=4;
end
pause
answer=questdlg('Would you like dessert tonight?','Hello','Yes please','No thank you','We are good for the night','No thank you');
if answer==1
desserts=menu('What dessert can I interest you in?','Chocolate Cake & Ice Cream','Skillet Cookie & Ice Cream','Fried Banana & Ice Cream');
switch desserts
case 'Chocalate Cake & Ice Cream'
disp([desserts ' coming right up!'])
desserts=1;
case 'Skillet Cookies & Ice Cream'
disp([desserts ' coming right up!'])
desserts=2;
case 'Fried Banana & Ice Cream'
disp([desserts ' coming right up!'])
desserts=3;
end
elseif answer==2
disp('I''ll bring you your check.')
dessert=0;
else answer=3
disp('I''ll bring you your check.')
dessert=0;
end
pause
disp('Thank you for coming to eat at Julize''s Restaraunt!. We will see you next time!')

Accepted Answer

Rik
Rik on 13 May 2020
If you don't open any figures, don't use close all. Also don't use clear all, use clear or clearvars instead.
If you read the documentation of the menu function you will notice it doesn't return a string, but an index.
food_list={'Steak & Fries','Fish & Rice','Hamburger & Fries','Shrimp & Rice'};
food=menu('What would you like to eat today?',food_list);
disp([food_list{food} ' coming right up!'])
Also, you don't alert the user that they should press a key in the command window.
Instead of disp you could also consider fprintf:
fprintf('%s coming right up!\n',food_list{food})

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!