Formatted Output
A common task in programming is to print the value of a particular variable to the display.It is also common for the values of the variables to be labeled with informative strings. For
example, when displaying the value of the light sensor on the screen, it might be useful to
print,
Light level: 33
to distinguish the light level number from, say, the sound level number. Generating the
printed statement above requires a mix of literal strings and variables and a knowledge of
format codes. Format codes are special strings, embedded in larger strings, that act as
place holders for variables. The formatted string for the light level output would be
"Light level: %d"
When used with nxtDisplayString() as in
nxtDisplayString(2,"Light level: %d", LightLevel);
the %d is replaced with the value of the variable LightLevel given as the third argument
which presumably had a value assigned to it earlier. Most of the display instructions can
take up to two variables. For example,
nxtDisplayString(7,"Light level: %d, sound level: %d",
LightLevel, SoundLevel);
will replace the rst %d with the value of LightLevel and the second %d with the value of
SoundLevel. Because the robot display is not able to t many characters on a single line,
only being able to use two variables in a formatted string is not a signicant limitation. In
fact, the last example is too wide to t on the robot's modest display. To get it to t, we
would have to adapt it as
nxtDisplayString(6,"Light level: %d", LightLevel);
nxtDisplayString(7,"Sound level: %d", SoundLevel);
The %d format code is useful for printing integer variables. It automatically opens enough
space in the output string to t the value of the variable inside. For the very particular
programmer, there is an extra parameter you can use with %d to force it to open up a xed
amount of space for the value of the variable no matter how many digits it might contain.
The syntax is %nd where n is an integer specifying the number of spaces to reserve for the
number. This is useful, for example, if the variable to be printed might have 1, 2, or 3 digits.
In that case, the format code %3d will always reserve 3 spaces for the variable. This is useful
when trying to line up a column of numbers. An example is in Listing 4.3.
task main () {
int n1 , n2 , n3; n1 = 100;
n2 = -3; n3 = 24;
nxtDisplayString (0," MotorA : %3d", n1 );
nxtDisplayString (1," MotorB : %3d", n2 );
nxtDisplayString (2," MotorC : %3d", n3 );
}
Listing 4.3: Formatted output. This snippet will print the numbers neatly in a right justied
column.
A word of caution, if there is not enough space reserved to hold the value of the variable,
then the space will be expanded to accommodate, potentially ruining any carefully crafted
formatting. The %d format code is for integer-type variables. The %f, %c, and %s codes are
used with
oat-, character-, and string-type variables respectively. Additional information
on format codes can be found in the RobotC On-line Support on the left side-bar under NXT
Functions ! Display section.
String variables are special because they do not need format codes to be included in
formatted output. Instead, the formatted output string can be constructed using the addition
operator, +. When two strings are added together, the result is a third string that is the
concatenation of the original strings reading from left to right. Consider the snippet of
instructions in Listing 4.4. Here we do not need a format code in order to include the string
variables in the formatted output. Instead, we build the string to be displayed by \adding"
together existing strings so that it appears the way we want. In this case, we want to display
string first = " Grace ";
string last = " Hopper ";
nxtDisplayString (0, first + " " + last );
wait1Msec (1000);
Listing 4.4: Concatenation of strings and the string addition operator, +.
the rst name followed by the last name with a space character in between. Adding the
three pieces together in the correct order, left to right, gives us the desired result.
Another important distinction between strings and character datatypes is how literal
values are recognized by the compiler. A literal string must be surrounded by double-
quote characters, ". A literal character must be surrounded by apostrophe characters, '.
Sometimes using one when we mean to use the other will cause an error. Unfortunately,
sometimes those kinds of errors are not detected by the compiler and only manifest as
misbehavior when the program is executed.
No comments:
Post a Comment