Jen
Jen

Reputation: 1

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:

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:

No case may be ran twice. The script will output something like 'That case has been run already' should this happen. Once all cases have been entered, I want to print something like 'All cases have been entered'.

Here is what I have tried so far:

  counter1 = 0;
  counter2 = 0;
  counter3 = 0;
  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 counter1>=1 || counter2>=1 || counter3>=1
      disp('That case has been run already');
  elseif 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);
  else counter1==1 && counter2==1 && counter3==1;
  end
  end
  disp('All cases have been entered!')

The only thing I can't seem to get to work now is this part:

  if counter1>=1 || counter2>=1 || counter3>=1
      disp('That case has been run already');

It seems to be ignored entirely. Any suggestions?

Upvotes: 0

Views: 47

Answers (1)

rayryeng
rayryeng

Reputation: 104484

Do away with all of your counter variables. Have boolean / logical flags instead that indicate when a particular case has been performed. Also, you would want to check if a particular case has already been performed inside each case itself. Don't do this as a separate external if statement. This is probably why that statement you originally wrote isn't working. As such, do something like this. I'll put %//NEW where I have added in new code:

  case1 = false; %// NEW
  case2 = false; %// NEW
  case3 = false; %// NEW
  while ~case1 || ~case2 || ~case3 %// NEW: While at least one of the cases has not been run...
      x = input('Please enter an x value > 0: ');
      while x < 0
          x = input('Invalid! Please enter another x value > 0: ');
      end

      if x <= 7 %// NEW
         if case1 %// NEW: Check if case #1 has already been run
             %// If it has, show this to the user, then continue in the loop
             fprintf('Case #1 has already been run!\n');
             continue; %// NEW - Continue through the loop.  Don't do anything else
         end

         case1 = true; %// NEW - Set to true if we haven't run this case already
         y = x.^3 + 3;
         fprintf('Case 1: y = %d \n',y);

      elseif 7<x && x<=12
         if case2 %// NEW - Repeat like Case #1 here
            fprintf('Case #2 has already been run!\n'); %// NEW
            continue; %// NEW
         end

         case2 = true; %// NEW
         y = (x-3)./2;
         fprintf('Case 2: y = %d \n',y);

      elseif x>12
         if case3 %// NEW - Repeat like Case #3 here
             fprintf('Case #3 has already been run!\n'); %// NEW
             continue; %// NEW
         end

         case3 = true; %// NEW
         y = 4.*x+3; 
         fprintf('Case 3: y = %d \n',y);
       end %// End if
   end %// End while
   disp('All cases have been entered!') %// Display once all cases have been entered

Here's a sample run for you showing that it works:

Please enter an x value > 0: -1
Invalid! Please enter another x value > 0: 3
Case 1: y = 30 
Please enter an x value > 0: 2
Case #1 has already been run!
Please enter an x value > 0: 8
Case 2: y = 2.500000e+00 
Please enter an x value > 0: 10
Case #2 has already been run!
Please enter an x value > 0: 14
Case 3: y = 59 
All cases have been entered!

I put in -1 to try and see if it rejects negative numbers, which is does. I put in a number that's < 7, which is 3. It enters the first case successfully. I try to put in another number that's < 7, which is 2. It gives the message saying Case #1 has already been run. I try to put in a number that's between 7 and 12... so I try 8. It generates Case #2 accordingly. I try again with 10, and it says that Case #2 has already been run. Lastly, I try 14, which is > 12, it generates Case #3 and stops as all cases have been successfully run.

This I believe is what you were looking for.

Upvotes: 2

Related Questions