Previous | Next | Table of Contents | Index | Program List | Copyright

9.7 Tricks of the Trade: Common Programming Errors

A variable of type String(1..N) is a string variable of length N (a constant). A string variable of length N can be assigned a string value consisting of exactly N characters. If a string value that is too short or too long is assigned to a string variable, Constraint_Error is generally raised at run time; the error may not have been detected by the compiler. If one string variable is assigned to another, they must both be the same length. You can use string slicing to ensure that the lengths agree. Also keep in mind that if S is a string of length 1, then

    S := 'A';
is not valid because S is a string and 'A' is a character.

When reading a string, do not forget that the Ada.Text_IO.Get procedure for strings reads exactly the number of characters called for by the string length. If you enter fewer characters, even if you press RETURN, the program will wait for you to enter the remaining characters.

File processing in any programming language can be difficult to master; Ada is no exception. The name, which will be used as a file variable in the program, will usually differ from the actual directory name of the associated disk file. All file names must be declared as variables (type Ada.Text_IO.File_Type), and associated with the corresponding disk file using a Ada.Text_IO.Create or Ada.Text_IO.Open procedure call statement.

The Ada.Text_IO.Get procedures can be used only after a file (other than standard input) has been opened for input. Similarly, the Ada.Text_IO.Put procedure can be used only after a file (other than standard output) has been created for output. Be sure to specify the file name as the first Get or Put parameter; otherwise, standard input (keyboard) or standard output (screen) is assumed. Ada.Text_IO.End_Error is raised if a Get operation is performed when the file position pointer for a file has passed the last file component. Also, when you use function Ada.Text_IO.End_Of_Line or Ada.Text_IO.End_Of_File to control data entry, don't forget to include the name of the data file as the function argument.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.