Input and Output

Most programs need to interact with their users, both to ask them what they want and to present the results when they are available. The computer term for this topic is I/O (short for “input/output”). We'll start by getting information from the keyboard and displaying it on the screen; later, we'll go over the more complex I/O functions that allow us to read and write data on the disk.

The program in Figure 3.20 displays the very informative text "This is a test and so is this.". The meaning of << is suggested by its arrowlike shape: The information on its right is sent to the “output target” on its left. In this case, we're sending the information to a predefined destination, cout, which stands for “character output”.[34] Characters sent to cout are displayed on the screen.

[34] The line #include <iostream> is necessary here to tell the compiler about cout and how it works. We'll get into this in a bit more detail shortly.

Figure 3.20. Some simple output (codeasic01.cpp)
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1;
    string s2;

    s1 = "This is a test ";
    s2 = "and so is this.";
    cout << s1;
    cout << s2;

    return 0;
}

This program will send the following output to the screen:

This is a test and so is this.

So much for simple output. Input from the keyboard can be just as simple. Modifying our little sample to use it results in Figure 3.21.

Figure 3.21. Some simple input and output (codeasic02.cpp)
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1;
    string s2;

    cin >> s1;
    cin >> s2;

    cout << s1;
    cout << "";
    cout << s2;

    return 0;
}

As you might have guessed, cin (shorthand for “character input”) is the counterpart to cout as >> is the counterpart to <<; cin supplies characters from the keyboard to the program via the >> operator.[35] This program will wait for you to type in the first string, ended by hitting the ENTER key or the space bar.[36] Then it will wait for you to type in the second string and hit ENTER. Then the program will display the first string, followed by a blank, and then the second string.

[35] In case you were wondering, cout is pronounced “see out”, while cin is pronounced “see in”.

[36] The reason a space ends a string is that the designers of the C++ standard library thought that would be more convenient than having everything up to the end of a line read into one string. I don't agree with this, but the advantages of using the standard library generally outweigh its disadvantages, so we just have to put up with any inconveniences that it causes.

Susan had some questions about these little programs, beginning with the question of case sensitivity:

Susan: Are the words such as cout and cin case sensitive? I had capitalized a few of them just out of habit because they begin the sentence and I am not sure if that was the reason the compiler gave me so many error messages. I think after I changed them I reduced a few messages.

Steve: Everything in C++ is case sensitive. That includes keywords like if, for, do, and so on, as well as your own variables. That is, if you have a variable called Name and another one called name, those are completely different and unrelated to one another. You have to write cin and cout just as they appear here, or the compiler won't understand you.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset