Beginner Question -- IF loop (code at beginning of post)

조회 수: 1 (최근 30일)
Dominic
Dominic 2013년 3월 26일
% 1. Greetings
fprintf(['\n Welcome! This program integrates and plots a 2D function of x. You may now choose your integration method.' ...
' \n Enter ''1'' for Rectangle Rule; ''2'' for Trapezium Rule; and ''3'' for Simpson''s Rule. \n\n'])
C = input ('Enter your choice = '); % Variable 'C' is chosen as 'C' for Choice
if C == 1;
run Rectangle_Rule
elseif C == 2;
run Trapezium_Rule
elseif C == 3;
run Simpsons_Rule
else
fprintf(['\n ------------------------------------------------------------------------------------------------------------' ...
'\n OPTION INVALID. PLEASE TRY AGAIN.'...
'\n ------------------------------------------------------------------------------------------------------------ \n'])
run Numeric_Integration
end
I want the fprintf and re-run of numeric integration to work at all cases. The code above works fine when what is input is a number or a an alphabet with apostrophes, e.g., 'code', 'A'; but if it is just a letter with no apostrophes, it doesn't *. For example, I try to enter *test This message appears:
Error using input
Undefined function or variable 'test'.
Error in Numeric_Integration (line 7)
C = input ('Enter your choice = '); % Variable 'C' is chosen as 'C' for Choice
How do I go around this problem?
Also, is there anyway that when it loops back to give the user another try to not show the welcome message again - this is what happens if I use run Numeric_Integration after else. Is there a code to make it just loop back to C = input ('Enter your choice = '); ?
Cheers
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 3월 26일
A note on your title: there is no such thing as an "if loop". There are "for loop" and "while loop", but "if" is an "if statement".
Dominic
Dominic 2013년 3월 27일
cheers for the clarification

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Kindig
Matt Kindig 2013년 3월 26일
For the input issue, use the option 's' to convert the input to string at the prompt, and then use str2double() to convert it to numeric if necessary. For example:
C = input('Enter your choice = ', 's');
C = str2double(C); %this will be NaN for *test and other strings, and thus will fall through to the "else" statement.
As for the looping question, wrap all of your statements after the welcome message with a while() loop, with some appropriate break condition. At the prompt, type
doc while
to bring up some useful help.
  댓글 수: 2
Matt Kindig
Matt Kindig 2013년 3월 26일
편집: Matt Kindig 2013년 3월 26일
By the way, you don't need the 'run' part of the commands-- just Rectangle_Rule, Trapezium_Rule, etc. will work fine.
Dominic
Dominic 2013년 3월 26일
Cheers

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Dominic
Dominic 2013년 3월 27일
It works now!
C = 0;
while C ~= 1 || 2 || 3;
C = input ('Enter your choice = ', 's');
C = str2double(C);
if C == 1;
Rectangle_Rule
elseif C == 2;
Trapezium_Rule
elseif C == 3;
Simpsons_Rule
else
fprintf(['\n ------------------------------------------------------------------------------------------------------------' ...
'\n OPTION INVALID. PLEASE TRY AGAIN.'...
'\n ------------------------------------------------------------------------------------------------------------ \n\n'])
end
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by