CS 1440 Lab 6

Cover sheetPrevious ActivityNext Activity                                                         Pre-Lab


Background

In Lab 2 you were introduced to the iostream library and its header file.  Including this in your C++ programs has allowed you to do basic input and output (I/O) using cin from the keyboard and cout to the monitor.  In your next lab you will delve deeper into I/O.  So, the time has come for some details that until now were unnecessary.

 

You learned about C++ data types in chapter 2, and should be comfortable with thinking of whole numbers as ints and real numbers (or numbers with fractional parts) as doubles.  Also, printable characters are chars.  Examples of chars are letters, special symbols like the dollar sign, and the 10 numeric digit characters.  The bool data type is used to hold truth values.  All of these types you've learned so far are C++ base data types.  But C++ lets us define our own more complex data types too!  These new data types are called "classes" and variables of these types are called "objects."  More on this next week, but for now you can know that cin and cout are just special variables of one of these new, more complex data types.  Specifically, cout is of type ostream and cin is of type istream.  You don't need to declare these special variables yourself because they are declared inside the iostream.h file you include!

 

Another new (non-base) data type in C++ is the string.  To use this data type, you must include the header <string> (notice that there is no  .h ) at the end.  A string is a sequence of characters treated as a single data item.  On the other hand, a char is just one character.  While you didn't know it, you've been using strings for weeks!  Actually, you've been using string constants in your programs.  Yes, that's what we call those collections of characters inside the double quotes.  The table below shows you some of the data types you use and what the constants for those types look like.

 

Data type

Example Constant

Example use

int

32

int  i = 32;

double

0.33

double  d = 0.33;

char

'J'

char  c = 'J';

string

"Hello world!"

string  s = "Hello world!";

 

You may remember from a previous lab that when reading a value into an int variable, cin  >>  automatically skips over any leading white space (spaces, tabs, newlines).  Also, when reading into that int variable, cin  >>  stops reading when it encounters a non-numeric character.  This works well for ints.  It also works similarly for doubles.  And fortunately, this is what we would expect.  So now consider a char variable.  If you are using cin  >>  to read a character into a char variable, what will happen if it encounters a number?  Well, it just reads one numeric character (it does not read a single digit number!!).  Does it skip any leading white space?  Or would it read the first white space character?  Well, the extraction operator (">>") does indeed skip white space when reading into char variables.  This makes it rather difficult to read (not skip) spaces, which sometimes you'll need to do.  So, how can we do it?  There is a special way to use cin to get a single character -- ANY character -- including white space characters like spaces, tabs, and newlines.  The special way is to use a function that is a part of cin.  The function is called cin.get and requires a single char reference parameter. So, to read a character (including white space) you would need to do something like:   cin.get(ch)  and to read a character (but not white space) you just use the familiar:   cin >> ch  input expression.

 

Okay, so you know that I/O with chars can be a little tricky.  You'll get some practice in your next lab to help you remember the differences.  Since strings are collections of characters, you may (should!) be wondering about those.  Well, you can certainly use cin  >>  to read into a string variable.

            string  s;
     cin  >>  s;

What happens?  Good question!  Leading white space is skipped and all characters are read into the string until a white space character is encountered.  Oh, you think you may need to read a string that contains spaces?  Wow!  You are really on the ball today!  So, clearly the extraction operator will not do what you want.  As with getting white space character data into a char variable, there is a function to get white space character data into string variables.  However, the function is a bit different.  The following shows you how to use it:

            string  s;
     getline(cin, s);

The getline function has two parameters, the istream (cin) and a reference to a string (s).  It reads characters from the keyboard putting them into the string parameter until a newline is reached.  Then it stops.

 

Just a little more, hang on -- it's worth it!!

 

Have you wondered how programs like the g++ compiler and the Pico editor work?  I mean those are just programs like the programs you've been writing.  They very possibly could be written in C++!!  Specifically, when you run the g++ program you tell it the Unix file that contains your C++ program.  In Pico, when you write out (save) you tell it a Unix file to write your data into.  How do these programs read and write to Unix files?  Can you do it too?  Of course, but you'll need to learn just a bit more.

 

C++ provides two more non-base data types for using Unix files INSIDE your C++ program.  The ifstream data type is an input file stream and the ofstream data type is an output file stream.  Both of these data types are defined in the fstream header file, so to use files in your program you'll need to include fstream as well as iostream.  Using files in your C++ programs requires doing the following things:

1.      You must include the fstream header file.

2.      You must declare an ifstream (or ofstream) variable.

3.      You must "connect" this variable with a real Unix file.

4.      You use the variable to read and write to the file.

5.      You must "disconnect" the variable from the Unix file when you are done.

 

The variable you declare in step 2 is the "internal" name, the C++ name, for the file.  Of course the file has its Unix file name as well so we call that the "external" name for the file.  Your program will use the internal name to actually read from and write to the file.  You can use any internal name you want, as long as it is a valid C++ variable name.  You "connect" the internal and external names by "opening" the file stream.  You disconnect them by "closing" the stream.  You read and write by using your internal name like you use cin and cout.  A quick example below and then lots of practice in lab.

     #include  <iostream>
     #include  <fstream>  // step 1 
     int  main()  
     {
            ofstream  fout;  // step 2 
            fout.open("output.txt");  // step 3  
                                      // Unix file: output.txt
            fout  <<  "Line 1" << endl    // step 4
                 <<  "Hello World"  <<  endl;
            fout.close( );  // step 5
      }

Exercises

1.      What included header file gives you access to cout and cin?

2.      Both  cin.get  and  cin >>  allow the input of character data.  Explain the difference between them.

3.      How can a C++ program read an entire line of input including the spaces in just a single step?

4.      What header file gives you access to the string data type?

5.      Is the string data type a base C++ data type?

6.      What header file gives you access to input file stream data types?

7.      Is the ifstream data type a base C++ data type?

8.      Using the C++ statement below, give (a) the file's external file name, and (b) the file's internal name.

          myin.open("example.txt");