Up to this point your programs have performed all I/O (input/output) using the printf and scanf (or some variation thereof) functions. This is because you have been using the C programming language and this is the only mechanism C provides for doing basic I/O. C++ provides a better, more flexible, mechanism: streams.
You can think of an I/O stream as just a long string of characters coming from the keyboard or going to the screen (there are actually other places these characters can come from of go to, but we will not consider them yet). Along the way, these characters can be manipulated by things called, appropriately, manipulators. Before we discuss manipulators, however, we should first get comfortable with the basic use of streams.
In C, if you want to print out a simple line of text you simply use the printf command, like: printf("Hello, world!");. In C++, however, you use the following:
cout << "Hello, world!";You can think of this as sending the string of characters "Hello, world!" to the cout stream (using the insertion operator <<). You may have noticed that we did not print out a new line character in this example. While you can use the familiar '\n' character, C++ provides its own mechanism. Modifying the previous example, if we wanted to move to the next line (i.e. print out a newline character) after the text is printed, we would say:
cout << "Hello, world!" << endl;
To print out a variable we simply give the name of the variable instead of a string. For example, cout << x; prints out the contents of the variable x. How this is done depends on the type of x.
We can also chain together several print commands. For example, we can do the three commands:
cout << "Value of x is "; cout << x; cout << endl;
with
cout << "Value of x is " << x << endl;
Another common task we would like to do is input values from the user. In C we use the scanf function to do this. For instance, if you wanted to read in an integer value and store it in a variable i, we would use: scanf(" %d", &i);. Just like in the case of output, input in C++ is also performed using streams. So, to read in an integer value and store it in a variable i in C++, we would use:
cin >> i;
which employs the extraction operator (>>). Notice that we did not have to specify the data type of the variable we wanted to read into. This is because C++ can figure out this information without our help and convert it to a string of characters. As with the insertion operator, a set of extraction operators can be chained together into one command.
The formatting can also be affected by changing the streams with a mechanism called a manipulator. A manipulator is in some sense a message that we send to a stream to cause it to change how it prints things out or read things in. We have already seen an example of a manipulator: endl. Remember that we used endl to print out a newline. Actually, endl is a manipulator that sends a newline character out over its output stream and also flushes flushes the output stream. There are several manipulators in the C++ language. The ones you will need to know for this lab are described below:
int i = 99; cout << "|" << setw(10) << i << "= i" << endl;will produce
| 99= ias output. setw() can also be used format input. For example, if we would like to limit the number of characters that can be read into a string to ten, we can do something similar to the following:
char str[10]; cin >> setw(10) >> str;
float pi = 3.14159; cout << "|" << setprecision(3) << pi << endl;will produce
|3.14as output.
int i = 99; cout << "|" << setw(10) << setfill('+') << i << "= i" << endl;will produce
|++++++++99= ias output.
Manipulators can also be chained together in a manner similar to the setfill() example. For instance, the following code:
float pi = 3.14159; cout << "|" << setw(10) << setfill('0') << setprecision(3) << pi << "=pi" << endl;will produce
|0000003.14=pias output.
Below is a program written in C that takes as input from the user one integer, floating-point and string value, and outputs them in a specific format. What you need to do is convert the C I/O statements to C++ streams. To do this you will need to #include two new header files:
#include <stdio.h> #define MAX_STRING_LENGTH 10 int main(void) { int i1; float f1; char string[MAX_STRING_LENGTH]; printf("Enter an integer value: "); scanf("%d", &i1); fflush(stdin); printf("The number you entered is: %5d\n", i1); printf("\n"); printf("Enter a floating-point (real) number: "); scanf("%f", &f1); fflush(stdin); printf("The number you entered is: %8.2f\n", f1); printf("\n"); printf("Enter a string (maximum of %d characters): ", MAX_STRING_LENGTH); scanf("%10s", string); fflush(stdin); printf("The string you entered is: %8s\n", string); return 0; }
Turn in a copy of your final source code along with a printout of your output.