MATLAB: Creating a script that will allow the user to input values repeatedly until each case has been entered

I'm trying to create a script that will allow the user to input x-values repeatedly until each case has been entered. I have three possible cases: Case 1 is entered when x<=7, case 2 is entered when 712. I want to use a while statement to error-check the user input, ensuring that x>0. Each time a case is entered, I want to print the case number & the created y-value (y = x^3 + 3 for case 1, y = (x-3)/2 for case 2, and y = 4*x+3 for case 3). No case may be ran twice (then the script will output something like 'That case has been run already'). Once all cases have been entered, I want to print something like 'All cases have been entered'.
Here's the script I have so far & I'm stuck:
x = input('Please enter an x value > 0: ');
while x < 0
x = input('Invalid! Please enter another x value > 0: ');
end
counter1 = 0;
counter2 = 0;
counter3 = 0;
if x<=7
counter1 = counter1 + 1;
y = x.^3 + 3;
fprintf('Case 1: y = %d \n',y);
elseif 7<x || x<=12
counter2 = counter2 + 1;
y = (x-3)./2;
fprintf('Case 2: y = %d \n',y);
elseif x>12
counter3 = counter3 + 1;
y = 4.*x+3;
fprintf('Case 3: y = %d \n',y);
end
if counter1==0 || counter2==0 || counter3==0
x = input('Please enter an x value > 0: ');
elseif counter1>=1 || counter2>=1 || counter3>=1
disp('That case has been run already')
elseif counter1==1 && counter2==1 && counter3==1
disp('All cases have been entered!')
end

Best Answer

Try this:
clc;
counter1 = false;
counter2 = false;
counter3 = false;
while counter1==0 || counter2==0 || counter3==0
x = input('Please enter an x value > 0: ');
while x < 0
x = input('Invalid! Please enter another x value > 0: ');
end
if x<=7
% It's case 1

if counter1
fprintf('Case 1 has been run already.\n');
continue;
end
counter1 = true;
y = x.^3 + 3;
fprintf('Case 1: y = %d \n',y);
elseif 7<x && x<=12
% It's case 1
if counter2
fprintf('Case 2 has been run already.\n');
continue;
end
counter2 = true;
y = (x-3)./2;
fprintf('Case 2: y = %d \n',y);
elseif x>12
% It's case 3
if counter3
fprintf('Case 3 has been run already.\n');
continue;
end
counter3 = true;
y = 4.*x+3;
fprintf('Case 3: y = %d \n',y);
end
end
fprintf('All cases have now been entered!\n')
Related Question