ورود

View Full Version : گرفتن ورودی از کاربر در یک برنامه کنسول



ehsan_faal
جمعه 09 شهریور 1397, 19:45 عصر
سلام.
من یه برنامه نوشتم که ssh کنه به روتر میکروتیک و آپتایم رو بخونه، حالا میخوام که تحت هر شرایطی که ssh موفقیت‌آمیز نبود از یوزر بپرسه که یه ورودی بده و دوباره اونکارو انجام بده.
راهی سر راست از goto به ذهنم نرسید.
و اینکه گرفت ورودی از کاربر هم معضلی شده برای من.
این کدمه.
ممنون میشم تجربه‌هاتون رو به اشتراک بذارین با بنده.
Program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

Uses
System.SysUtils,
System.StrUtils;

Var
userInput: Char;
str: String;

Label
again, getOut;

Begin
again:
userInput := '0';
Write('Please Choose:'#10#13'c|C Continue'#10#13'a|A Abort'#10#13's|S Shut The Alert Down'#10#13'You''r Choise: ');
Read(userInput);
Case UpCase(userInput) Of
'C':
Begin
Writeln('You chosed:', userInput);
Goto getOut;
End;

'A':
Begin

Writeln('You chosed:', userInput);
Goto getOut;
End;
'S':
Begin
Writeln('You chosed:', userInput);
Goto getOut;
End
Else
Begin
Writeln('Wrong Option: "', userInput, '"');
Goto again;
End;
End;
getOut:
Writeln('outside');
readln;
Readln;
End.

یوسف زالی
جمعه 09 شهریور 1397, 21:24 عصر
Begin
repeat
userInput := '0';
Write('Please Choose:'#10#13'c|C Continue'#10#13'a|A Abort'#10#13's|S Shut The Alert Down'#10#13'You''r Choise: ');
Read(userInput);
Case UpCase(userInput) Of
'C':
Writeln('You chosed:', userInput);

'A':
Writeln('You chosed:', userInput);


'S':
Writeln('You chosed:', userInput);


Else
Writeln('Wrong Option: "', userInput, '"');
End;
until UpCase(userInput) in ['C', 'A', 'S'];




Writeln('outside');
readln;
Readln;
End.

ehsan_faal
جمعه 09 شهریور 1397, 21:28 عصر
ممنون از پاسختون اما مشکل همچنان پابرجاست.
خیلی دنبال راه حل گشتم اما نمیدونم چرا به مرحله دوم حلقه که میرسیم read قاطی میکنه.
خروجی کنسول:


Please Choose:
c|C Continue
a|A Abort
s|S Shut The Alert Down
You'r Choise: w
Wrong Option: "w"
Please Choose:
c|C Continue
a|A Abort
s|S Shut The Alert Down
"ou'r Choise: Wrong Option: "
Please Choose:
c|C Continue
a|A Abort
s|S Shut The Alert Down
You'r Choise: Wrong Option: "
"
Please Choose:
c|C Continue
a|A Abort
s|S Shut The Alert Down
You'r Choise:

Pascal
دوشنبه 26 شهریور 1397, 20:21 عصر
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
Windows,
Messages,
SysUtils;

var
userInput: Char;
str,Str1: String;
hStdOut: HWnd;
ScreenBufInfo: TConsoleScreenBufferInfo;
Coord1: TCoord;
i: Integer;


Procedure CLS;
begin
GetConsoleScreenBufferInfo(hStdOut, ScreenBufInfo);
{the GetConsoleScreenBufferInfo API gets the size of
the buffer I need}
for i := 1 to ScreenBufInfo.dwSize.Y do
WriteLn('');
Coord1.X := 0;
Coord1.Y := 0;
SetConsoleCursorPosition(hStdOut, Coord1);
{SetConsoleCursorPosition API sets your cursor
to the Coord1, at 0, 0 the beginging}
Readln(str1);
end;



procedure test;
begin
writeln('Please Choose:'#10#13'c|C Continue'#10#13'a|A Abort'#10#13's|S Shut The Alert Down'#10#13'You''r Choise: ');
userInput := '0';

read(userInput);


Case UpCase(userInput) Of
'C':
Writeln('You chosed:', userInput);

'A':
Writeln('You chosed:', userInput);


'S':
Writeln('You chosed:', userInput);


Else
Writeln('Wrong Option: "', userInput, '"');
End;

end;

begin

while userInput<>'A' do
begin
cls;
test;
end;



end.