CPSC 120 LAB 12 | Back to Lab index page |
Lab must be completed and reviewed by Peer Instructor Seth Peery or Intructor Shawn Brenneman no later than Fri, Nov 16 by 2pm.
You should complete Lab 11. This lab shows how to set up an array of records. The attached program gives an example of how to use the Ada.text_io.end_of_file and Ada.text_io.end_of_line functions to get input from a file. Make a file that could be used for input formatted like the sample data below. The first line gives the weights for each assignment and starting with the second line a student name and set of scores is given for each student in the class. Create a file, containing the data below, call it lab12in.
Sample data
0.1 0.2 0.4
0.3
John Doe
85 78 90 75
Carol Smith
75 95 90 85
Joe Student 55 45 85 90
Mary Radford 95 90 85 60
Be sure to use exactly 20 spaces for the Name (add blanks to the end, if necessary)
Input the program (attached). Compile it. You can run it using the input data in lab12in, by entering Lab12 < lab12in. Examine the results. This should give you a good example of how to work with these functions.
The end_of_line and end_of_file functions
in Ada.Text_IO become true when the next thing to be read is the end of file
or end of line marker. When you create a file, the editor puts end of line and
end of file markers and the end of each line and at the end of the file. However,
you do not see them on the screen. The rules for how Ada input statements work
in relation to these marks are listed below. I suggest you understand why and
how this program works.
Get (character variable)
skips any line terminators then reads one character, a blank counts
as a characterGet (string variable)
gets the number of characters defined for the string, will skip line
terminators to do this if necessary
Get (numeric or enumerated)
skips over leading blanks tab characters, and line terminators then reads
characters as long as they continue to meet the syntax of the type,
the character that causes the reading to stop remains available for the next input
operationGet_Line(string variable, integer variable)
reads characters including blanks up to the length of the string. Reading stops
if the string's length is longer than the current line (i.e., if a line terminator
is encountered). The line terminator is then skipped, that is , the equivalent of a
Skip_Line is executed. If the input line is longer than the string, the remaining
characters in the line remain available for the next input operation.
with ada.text_io;
with ada.float_text_io;procedure getvarinput is
---------------------------------------------------------
-- this procedure gets data, when the number of inputs is
-- not known
---------------------------------------------------------type wgtarray is array (1..10) of float; -- array of weights for assignments
type scorearray is array (1..10) of float; -- array of scores on assignments
type sturec is record -- record for each student, name & scores
name : string (1..20);
scores : scorearray;
end record;type sturecarray is array (1..20) of sturec; -- array of student records
wgts : wgtarray;
stusc : sturecarray;
j : positive; -- counters
nowgt : natural; -- number of weightsbegin -- getvarinput
-- this section reads in the wgts, until end of line is true
-- at the end of the loop the number of wgts read will be = nowgtnowgt:=0;
while not ada.text_io.end_of_line loop
nowgt:=nowgt+1;
ada.float_text_io.get(wgts(nowgt));
end loop;
-- skips to the next line of the input file, after reading all the weights
ada.text_io.skip_line;-- read the name and scores for each student put them in the array
-- of student recordsj:=1; -- j counter used for the
-- index of the student recordwhile not ada.text_io.end_of_file loop
while not ada.text_io.end_of_line loop
ada.text_io.get(stusc(j).name);
-- get the scores, scores = the number of wgts.for k in 1..nowgt loop
ada.float_text_io.get(stusc(j).scores(k)); -- get the score
end loop;
ada.text_io.skip_line;
j:=j+1; -- increment the counter, next student
end loop; -- end loop getting the scores for one studentend loop;
----------------------------------------------------------
-- Some output just to see if we got the input in correctly
for i in 1..j-1 loop
ada.text_io.new_line;
ada.text_io.put(stusc(i).name);
for k in 1..nowgt loop
ada.text_io.put(" ");
ada.float_text_io.put(stusc(i).scores(k),fore=>2,aft=>1,exp=>0);
end loop;
end loop;
end getvarinput;