JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
  Previous Section Next Section

Stream I/O

I love printf(). Nothing is more pure than

printf("\nGive me some sugar baby.");

The only problem with printf() is all those format specifiers, like %d, %x, %u, and so forth. They're hard to remember. In addition, scanf() is even worse because you can really mess things up if you forget to use the address of a variable for storage. For example:

int x;

scanf("%d",x);

This is incorrect! You need the address of x or &x, so the correct syntax is

scanf("%d",&x);

I'm sure you have made this mistake. The only time you don't have to use the address operator is when you're working with strings, because the name is the address. In any case, this is why the new IOSTREAM class was created in C++. It knows the types of the variables, so you don't need to tell it anymore. The IOSTREAM class functions are defined in IOSTREAM.H, so you need to include it in your C++ programs to use it. Once you do, you will have access to the streams cin, cout, cerr, and cprn, as shown in Table D.1.

Table D.1. C++ I/O Streams
Stream Name Device C Name Meaning
cin Keyboard stdin Standard input
cout Screen stdout Standard output
cerr Screen stderr Standard error
cprn Printer stdprn Printer

Using the I/O streams is a bit weird because they're based on the overloaded operators, << and >>. These operators normally signify bit shifting in C, but in the context of the I/O streams, they're used to send and receive data. Here are some examples of using the standard output:

int i;
float f;
char c;
char string[80];

// in C
printf("\nHello world!");

// in C++
cout << "\nHello world!";

// in C
printf("%d", i);

// in C++
cout << i;

// in C
printf("%d,%f,%c,%s", i, f, c, string);

// in C++
cout << i << "," << f << "," << c << "," << string;

Isn't that cool? You don't need any type specifier because cout already knows the type and does it for you. The only really weird thing about the syntax is the way C++ allows you to concatenate the << operator to the end of each operation. The reason for this is that each operation returns a stream itself, so you can add << forever. The only downside to using streams for simple printing is the way you have to separate variables and string constants, like the "," that separates each variable. However, you can put the << on each line if you wish, like this:

cout << i
     << ","
     << f
     << ","
     << c
     << ","
     << string;

Remember, in C and C++, whitespace is discarded, so this coding is legal.

The input stream works in much the same way, but with the >> operator instead. Here are some examples:

int i;
float f;
char c;
char string[80];

// in C
printf("\nWhat is your age?");
scanf("%d",&i);

// in C++
cout << "\nWhat is your age?";
cin >> i;

// in C
printf("\nWhat is your name and grade?");
scanf("%s %c", string, &c);

// in C++
cout << "\nWhat is your name and grade?";
cin >> string >> c;

A little nicer than C, isn't it? Of course, the IOSTREAM system has a million other functions, so check it out.

      Previous Section Next Section
    



    JavaScript EditorAjax Editor     JavaScript Editor