How can I rerun my code if my given statement is not followed by the user?

5 views (last 30 days)
%% 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.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Feb 2022
%% Initiation of Version 2
disp('Version ²')
while true
% 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.')
break
case 2
tuition_cost = science_tuition_cost;
disp('Unfortunately!!! You do not have enough saved for the science program.')
break
case 3
tuition_cost = engineering_tuition_cost;
disp('Unfortunately!!! You do not have enough saved for the engineering program.')
break
otherwise
disp('Incorrect input entered. Only (1 2 3) allowed. Try again!')
%do NOT break here
end
end
  3 Comments
Walter Roberson
Walter Roberson on 28 Feb 2022
It is a common programming pattern.
There are two common ways of handling errors when asking for input:
  1. ask for the initial input; check it, and if it is not valid, display an error message, then go back and display the original input prompt again; Versus
  2. ask for the initial input; check it and if if it is not valid, then go back to prompt again, but displaying a different prompt message than the original one
Either way, it is better to have the code only check the responses in one place, as otherwise there is the risk that if the behaviours were to change, that you might accidentally update the behaviour in only one of the places.
It is a common pattern to use:
  1. start a loop
  2. prompt for input
  3. check the inputs; if they were invalid, complain
  4. if they were valid, act on them and then leave the loop
  5. end of loop
But it is also entirely valid to instead program
  1. start a loop
  2. prompt for input
  3. check the inputs; if they were invalid complain
  4. if they were valid, then leave the loop
  5. end of loop
  6. act on the inputs -- which at this point are known to be valid (unless something in the processing of the inputs marked that the user asked to quit or that the user ran out of chances)
That second form has the advantage that you can often write the prompt / validation into a function, something like
[program_str, userquit] = get_input_str({'1', '2', '3'}, 'Please select a program to optimize (1-Art, 2-Science, 3-Engineering):', 'Incorrect input entered. Only (1 2 3) allowed. Try again!');
and then your main code does not need to loop at all: the loop is all inside get_input_str (which, for clarity, is a function you would write.)

Sign in to comment.

More Answers (1)

David Hill
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

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!