Ada get Character input and check if it is a valid input -
with ada.text_io, ada.integer_text_io, ada.characters.handling; ada.exceptions; utilize ada.exceptions; utilize ada, ada.text_io; connectfour; procedure main bundle board new connectfour; utilize board; col : character; function checkinput (input : character) homecoming boolean ins : character := input; begin ins := ada.characters.handling.to_lower(ins); if ins = 'a' or ins = 'b' or ins = 'c' or ins = 'd' or ins = 'e' or ins = 'f' or ins = 'g' or ins = 'h' homecoming true; end if; homecoming false; end checkinput; begin board.initialize; board.print; while (not board.isfull) loop loop put("player"&integer'image(board.turn)&": "); ada.text_io.get(col); exit when checkinput(col); end loop; exit when col = '0'; text_io.new_line; text_io.put (""); board.play(col); end loop; end main;
so when run programme get:
player 1: --> (i entered character 'a' , clicked enter)
then maintain getting error right after, @ line "ada.text_io.get(col);"
raised ada.io_exceptions.data_error : a-tiinio.adb:86 instantiated @ a-inteio.ads:18
what want do, single character input user , check if within range .. h, if yes, exit loop, otherwise maintain asking...
i cannot find out issue is...
i allow user come in lowercase or uppercase characters, , convert uppercase lowercase , perform check.
please help...
i not sure how read in single character....
okay exception message clearer (and there ways stack traces when 1 happens let's work we've got...)
locate a-tiinio.adb /usr/lib/gcc/x86_64-linux-gnu/4.9/rts-native/adainclude/a-tiinio.adb
which is
package body ada.text_io.integer_io
and line 86 exception raised in
procedure (item : out num; width : field := 0)
details don't matter (yet) cannot see calls ada.text_io.integer_io.get.
so suspicion code shown working (as can expected : note there no way exit when col = '0';
col
outside .. h) , there phone call (this time integer_io.get) buried in board.play
. easy test printing col= before calling board.play.
minor style comment:
function checkinput (input : character) homecoming boolean ins : character := input; begin ins := ada.characters.handling.to_lower(ins);
can simplified
function checkinput (input : character) homecoming boolean ins : character := ada.characters.handling.to_lower(input); begin
character ada
No comments:
Post a Comment