필터 지우기
필터 지우기

input() only prints its prompt on first iteration of loop

조회 수: 7 (최근 30일)
Andrew
Andrew 2023년 9월 1일
댓글: Alexander 2023년 9월 2일
I'm attempting to create a program that gives the output of a chosen function when the user inputs the function's number. The program will continue forever prompting and giving answers. The first time the loop runs, I enter in '1', and everything works as I expect. The second and subsequent times, input() does not output its prompt. Input() still runs and will accept input but doesn't display anything. What did I do wrong here?
close all
clear
%% Problem Selection Flow Control
while true
clc
p_num = input("Please enter a problem number. Type 'exit' to exit program.","s");% Promt user for problem number
if(p_num == "exit")% Check if user wants to exit program
clc
break
end
% Tests to see if user input invalid data. If so, notify user and
% restart loop.
try
eval(strcat("result = problem",p_num,';'));% Evaluates the function associated with the selected problem
catch
fprintf("\nThe entered problem doesn't exist. Press any key to try again.");
pause
continue
end
clc
fprintf(strcat("The answer for problem ",p_num," is %f"),result(1));
fprintf(" and %f",result(2:end));% Output the result from the selected problem
fprintf("\nPress any key to continue.");
pause% Waits until user presses any key
end
%% Problem 1
function result = problem1()
A1 = atan(2);
A2 = atan(100);
result = [A1 A2];
end
  댓글 수: 5
Dyuman Joshi
Dyuman Joshi 2023년 9월 2일
It's working properly in my R2021a MATLAB App.

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

답변 (0개)

카테고리

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

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by