C++ Seminar: 1094 Session 7
Handouts: "C++ Strings and Things" handout (with
filename_test.cpp printout on the back)
Your goals for today: Finish up Session 7, do a bit more of Day 13
and some of Day 17, then an example of C++ strings used to create
filenames.
Finish Up Session 7
If you didn't finish Session 7 (that may be just about everyone),
go back and try to run through the rest of it. At this stage, getting
exposure is more important than getting all the fine points.
Day 13: Arrays and Character Variables
This is a continuation of the discussion of arrays in Day 13. (If you
haven't done that section from Session 6, go back and do it now.)
A special type of array used for characters is called a "C-style
string". It is designated with the keyword char (instead
of int or double). You can declare a char
array of length 80 like this:
char buffer[80];
or declare and initialize without specifying the length like this:
char Greeting[] = "Hello World";
In the second example, the array Greeting is 12 bytes long, one
for each character you see and one more for a "null character" at the
end, which marks the end of the array.
[Note: In the last section below we'll look at using the C++ string
class, which is also used for text.]
- Look at List1310.cpp first. It declares a character
array to read in a "string" (this just means a sequence of characters)
from the user. Run it and enter your first name. Then run it again
and enter your full name. What's the problem? (You're not expected
to fix it here!)
- A solution to the problem is given in List1311.cpp by
using the get method (i.e., function) of cin.
It takes two arguments (actually three in general!), the name of
a char array and the maximum number of characters to read in.
The unused third argument is an optional delimiter that signals where
to stop (by default it is the newline character, which means it
terminates with a return). Try it out with full sentences. Change the
maximum
number of characters to 10 and see what happens when you try a string
of length 20 or so. What happens if the maximum number of characters
allowed (and entered) is greater than the size of the char
array?
- There are various library functions that can be used to manipulate
C-style strings. To access them, we need to include string.h.
One example is strcpy, which is used to copy one string into
another. Check out List1312.cpp for an example. Figure out
from the example how it works and test it by getting a string from the
user and copying it into String1. A similar function,
strncpy, which lets you specify the maximum number of
characters copied, is
illustrated in List1313.cpp. Try changing MaxLength
to something like 5.
Day 17: Streams
Day 17 is about streams, which we've been using all along for input from
the keyboard and
output to the screen. There is much to cover here, with many example
programs available from the web page. We'll pick a subset of useful
things to look at today; we recommend you explore the others!
-
List1701.cpp is a reminder that cin and cout
are smart enough to figure out what type of variable is being input or
output, and to store or print it appropriately. Just take a quick
look.
- We haven't tried to control what the output looks like; we've just
taken what we're given. Let's see what we can adjust. First is
List1712.cpp, which shows how to set the width of output.
Try changing the values in the width to understand what it is doing.
Then look at List1713.cpp to see how we can "fill" the result.
Try different fill characters.
- In List1716.cpp, we see an example of opening a file
and writing to it. There are several new features here. Your task
is to step through the code and figure out how to open a file with the
same name as your own with a "_" between your names (e.g.,
Dick_Furnstahl) and write the numbers from 1 to 10 into it.
An example of how to append to an existing file is in
List1717.cpp. Try it out on your "name" file.
Extra: Strings and Things
The handout "C++ Strings and Things" gives an abbreviated summary of how
to use the C++ string class with the goal of constructing
filenames for input and output. C++ strings are an alternative to working
with char arrays. On the back of the handout is a printout of
the file filename_test.cpp, which we'll examine here.
- Download filename_test.cpp and compile and run it. Then
take a look at the printout of the code and the output files that
are generated. Several things are illustrated one after another.
Following the first example in the code and the table in the handout,
add lines to declare a string called "my_string" and assign it a
phrase. Print it out to check that it worked. Then add additional
lines redefining my_string to a different word or phrase, and
print it out. (Note that the size of the
string adjusts automagically.)
-
Modify the code so that there is a loop running from 0 to 3 with index
variable j. For each j, open a file with a name that includes the current
value of j. Write "This is file j", where "j" here is the current value,
into each file and then close it.
-
Modify the code to input a double named alpha and open a filename with
3 digits of alpha as part of the name. (E.g., something like
output_alpha5.22_plot.dat if alpha = 5.21934.)
Output something appropriate to the file as part of your test.
C++ Seminar: 1094 Session 7.
Last modified: 08:23 am, November 26, 2006.
furnstahl.1@osu.edu