フィルターのクリア

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.
Alexander
Alexander 2023 年 9 月 2 日
R2021b U6

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops 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