问题
I wrote a function to clear the keyboard buffer, since I think I am having leftover input after a get, but it's just taking input forever.
here is the get(String(1..10)
--getString10--
procedure getString10(s : in out string10) is
Last: Integer;
begin
s := (others => ' ');
Get_Line(S, Last);
end getString10;
and here is the flush I made, pretty much copied from a wiki on clearing keyboard buffers
--flush--
procedure flush is
char : character;
more : boolean;
begin
loop
get_immediate(char, more);
exit when not more;
end loop;
end flush;
Whenever the flush is called, whatever I type is outputted on the screen until I exit the program.
Also, my getString10 function doesn't always wait on user input. For example, if I have
put("Enter a name: ");
getString10(name);
put("Enter a number: ");
getString10(number);
The output will be
Enter a name: Enter a number: exampleinput
I am using Ada 2005 on Gnat Programming Studio.
Updated with the entire main:
with Ada.Text_IO, Ada.Integer_Text_IO, BinarySearchTree;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure lab4 is
subtype String10 is String(1..10);
-- function "<"--
function "<"(TheKey: in String10; ARecord: in String10) return Boolean is
begin
for i in integer range 1..10 loop
if TheKey(i) <= ARecord(i) then
return true;
else
return false;
end if;
end loop;
return false;
end "<";
-- function ">"--
function ">"(TheKey: in String10; ARecord: in String10) return Boolean is
begin
for i in integer range 1..10 loop
if TheKey(i) >= ARecord(i) then
return true;
else
return false;
end if;
end loop;
return false;
end ">";
-- function "="--
function "="(TheKey: in String10; ARecord: in String10) return Boolean is
begin
for i in integer range 1..10 loop
if TheKey(i) /= ARecord(i) then
return false;
end if;
end loop;
return true;
end "=";
--getString10--
procedure getString10(s : in out string10) is
Last: Integer;
begin
s := (others => ' ');
Get_Line(S, Last);
end getString10;
--flush--
procedure flush is
char : character;
more : boolean;
begin
loop
get_immediate(char, more);
exit when not more;
end loop;
end flush;
package BST is new BinarySearchTree(String10, String10, "<", ">", "=");
Root, found : BST.BinarySearchTreePoint;
choice : integer;
nameTemp, phoneTemp : String10;
begin
BST.setRoot(Root);
loop
new_line;
put_line("Options:");
put_line("1 - Insert a record");
put_line("2 - Find a person iteratively and print their phone number");
put_line("3 - Find a person recursively and print their phone number");
put_line("4 - Traverse the tree from a person to a person");
put_line("0 - Quit program");
put("Choose an option: ");
get(choice); put(choice, 0); new_line;
case choice is
--case 1
when 1 =>
put("Enter the name: ");
get(nameTemp); put(nameTemp); new_line;
put("Enter the phone number : ");
get(phoneTemp); put(phoneTemp); new_line;
BST.InsertBinarySearchTree(root, nameTemp, phoneTemp);
--case 2
when 2 =>
put("Enter the name of the person to find: ");
get(nameTemp); put(nameTemp);
BST.FindCustomerIterative(root, nameTemp, found);
if BST.isNull(found) then
new_line;
put("Customer not found!");
else
new_line;
put("The phone number is: ");
put(BST.CustomerPhone(found));
end if;
--case 3
when 3 =>
put("Enter the name of the person to find: ");
get(nameTemp); put(nameTemp);
BST.FindCustomerRecursive(root, nameTemp, found);
if BST.isNull(found) then
new_line;
put_line("Customer not found!");
else
new_line;
put("The phone number is: ");
put(BST.CustomerPhone(found));
end if;
new_line;
--case 4
when 4 =>
put("Enter of the name of the person to start traversal at: ");
get(nameTemp); put(nameTemp);
BST.FindCustomerRecursive(root, nameTemp, found);
put("Enter then name of the person to stop traversal at: ");
get(phoneTemp); put(phoneTemp); --using phoneTemp for a name here
BST.FindCustomerRecursive(Root, nameTemp, found);
while BST.isNull(found) /= true loop
put_line("Name = " & BST.CustomerName(found));
BST.setNode(found, BST.InOrderSuccessor(found));
end loop;
--case 0
when 0 =>
exit;
--others
when others =>
put_line("Invalid choice!"); new_line;
end case;
end loop;
end lab4;
I switched out all of the getString10()s with get()s because I am trying to debug the threaded binary search tree. I am using an input file, so it's fine for now, I just can figure out why the other methods wouldn't work. All of the get calls on nameTemp and phoneTemp should be getString10() calls.
回答1:
You could use the get_line() function, which automatically clears the keyboard buffer.
with ADA.TEXT_IO; use ADA.TEXT_IO;
with ADA.TEXT_IO.UNBOUNDED_IO; use ADA.TEXT_IO.UNBOUNDED_IO;
with ADA.STRINGS.UNBOUNDED; use ADA.STRINGS.UNBOUNDED;
procedure MAIN is
type STRING_10 is new STRING(1..10);
procedure GET_STRING_10(S : in out STRING_10) is
BUF : UNBOUNDED_STRING;
begin
BUF := GET_LINE;
for I in STRING_10'range loop
if I <= LENGTH(BUF) then
S(I) := ELEMENT(BUF, I);
else
S(I) := ' ';
end if;
end loop;
end GET_STRING_10;
S : STRING_10;
begin
GET_STRING_10(S);
PUT_LINE(STRING(S));
end MAIN;
EDIT after reading the whole main:
You should insert a SKIP_LINE;
after GET(CHOICE);
. Then you can replace every GET
by a GETSTRING10
in your different cases.
In general, a get must always be followed by a skip_line. There's no need to do this with a get_line, thus you don't have to modify you getstring10 procedure.
来源:https://stackoverflow.com/questions/13758078/clearing-the-keyboard-buffer-in-ada