How can I rerun my code if my given statement is not followed by the user?
Show older comments
%% Initiation of Version 2
disp('Version 2')
% Prompt user to input value from given options
program_str = input('Please select a program to optimize (1-Art, 2-Science, 3-Engineering): ','s');
program_optimization = program_str - '0';
switch program_optimization
case 1
tuition_cost = art_tuition_cost;
disp('Congratulations!!! You have saved enough for the arts program.')
case 2
tuition_cost = science_tuition_cost;
disp('Unfortunately!!! You do not have enough saved for the science program.')
case 3
tuition_cost = engineering_tuition_cost;
disp('Unfortunately!!! You do not have enough saved for the engineering program.')
otherwise
while program_optimization ~= 1 || program_optimization ~= 2 || program_optimization ~= 3
disp('Incorrect input entered. Only (1 2 3) allowed. Try again!')
program_str = input('Please select a program to optimize (1-Art, 2-Science, 3-Engineering): ','s');
program_optimization = program_str - '0';
end
if savings(end) >= art_tuition_cost
disp('Congratulations!!! You have saved enough for the arts program.')
elseif program_optimization == 2
disp('Unfortunately!!! You do not have enough saved for the science program.')
elseif program_optimization == 3
disp('Unfortunately!!! You do not have enough saved for the engineering program.')
end
end
My issue starts from under "Version 2". Any help would really be appreciated. Thank you.
2 Comments
Benjamin Thompson
on 28 Feb 2022
Do you mean asking the user for input again if they do not enter 1, 2, or 3?
Shane Jacob
on 28 Feb 2022
Accepted Answer
More Answers (1)
David Hill
on 28 Feb 2022
program_str ='a';
while ~ismember(program_str,'123');
program_str = input('Please select a program to optimize (1-Art, 2-Science, 3-Engineering): ','s');
end
1 Comment
Shane Jacob
on 28 Feb 2022
Edited: Shane Jacob
on 28 Feb 2022
Categories
Find more on Functions 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!