Thursday, July 15, 2010

C++: The meahning of "cout" and "cin"

In continuation with our series on explaining the sample hello World
program, I'll talk about how you can display text and get user input. Last
time, we stopped at the actual code that tells the computer that it's the
start and end of our program. Now, let's go into things in between.

The next line that we've seen was:
cout << "Hello, World!" << endl;
The key is the word "cout". This is read as "console out." This means that
we want to display information to a computer monitor, like printing to a
printer or listening to a sound (when the music player "outputs" i.e. plays
the sound). This is the keyword that we'll use to send messages to the
screen so that users will know what the program is doing. Before I end this
discussion, make sure to include two less-than signs after cout (<<) -
called "stream insertion operator". I'll discuss what that is later.

Next, the text within quotations marks is the actual thing that the program
will display for us. For example, if we want our computer to display our
name, we would say in psudo code (an English rendition of our code):
Console out, "Hi, my name is Joseph.";
When the program is "executed" i.e. runs, it'll say:
Hi, my name is joseph.
But how do we insert a new line so that the screen is not "overcrowded" with
messages? There are two ways: using "endl" (new line operator) or using "\n"
(ASCII (American Standard Code for Information Interchange) code for new
line). But, as with cout, you need to provide two less-then signs, but this
time, you put it between the text and the "endl" sign; or, if you elect to
use "\n", you don't have to put any less-than signs - all you need is a
semicolon after the quote.
So, here are the two variations - first using "endl" and the second using
"\n":
cout << "Hi, my name is Joseph." << endl;
and:
cout << "Hi, my name is Joseph.\n";
Both lines will do the same thing. It's really up to you to use either
method - I personally use the first method for ease of reading my code.

Now, how do we get user input? As opposed to "cout", there is another one
called "cin" - read as "console in". This allows a user to type something on
their keyboard so that the program can work with it. As opposed to using two
less-than signs, we use two greater-than signs (>>) to differentiate from
cout (called stream extraction operator, but more on that later). But here's
a twist: you need to provide the actual data that the computer will work
with, and that'll be covered after I post a very important tip on commenting
your code, but I'll give you a preview:
Suppose if I want the computer to store my age (a number), and ask it to
print out my name and my age. I'd do the following:
int my_age;
cout << "Hi, what's your age?" << endl;
cin >> My_age;
cout << "I see. You are " << my_age << " years old." << endl;
The thing about this code will be covered later when I discuss variables and
data types - after all, programs do need to work with data that the user
provides.

There is another keyword (or operator) called "cerr" which is used to
display error messages, but I think cout and cin is enough - I think it's
time to let you go. By the way, these operators - cout, cin and cerr - comes
from iostream library. And, you would put the name of these "default" or
built-in libraries and surrounding them with "arrows" like:
#include <iostream>
When we create classes and our own modules, we'll differentiate between
built-in ones and our own using a quote (the quotes are used to say that we
are putting our own libraries).

Stay tuned for an important announcement from our "blind Highlander..."
// JL

No comments:

Post a Comment