Clear Filters
Clear Filters

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

7 views (last 30 days)
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 Comments

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!