Filter löschen
Filter löschen

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

7 Ansichten (letzte 30 Tage)
Andrew
Andrew am 1 Sep. 2023
Kommentiert: Alexander am 2 Sep. 2023
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 2 Sep. 2023
It's working properly in my R2021a MATLAB App.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by