Sunday, October 13, 2013

C++ Tutorial #6: Input and Output (I/O)

***Note: In computer science, input and output is almost always abbreviated as "I/O".***

The Basics of the Input/Output System

This may bring back memories of math class.   The concept of I/O is simple: you enter something for the input, and you receive something back—the output.  You place your change into the vending machine and get a snack or drink.  You punch equations into a calculator and you receive answers.  You enter your password and gain access to your online account.  Computers are quite good at I/O, the simplest form being taking what the user enters and spitting it back out.  You type a-whoop-a-dee-doo, and the program reads it and repeats a-whoop-a-dee-doo, either by itself or in special context. That’s what this section is about.

Here's Another Program For You to Stare At

Source code for the very polite and proper “Introductions.cpp”.  It's also the first full program in this tutorial with comments!

#include <iostream>
#include <string>   //notice the second "#include"
using namespace std;  //Remember, this line's important too!

int main() 

string name;

cout << "What's your name?  " << endl;   
//I added a couple of spaces on the end, just for formatting.
cin >> name;   //OH CRAP THAT'S NEW
cout << endl;  //equivalent of hitting "Enter"
cout << "It's very nice to meet you, " << name << "." << endl;

return 0;

So let’s break that down…

  • #include <iostream>: "Include text from the file iostream, which tells the compiler what cout and cin mean."
  • #include <string>: "Include text from the file string, which tells the compiler what a string variable is."
  • using namespace std: Sometimes (like when I tested the code just now) I forget this line.  If your program errs, double-check to make sure this guy is in place.
  • int main(): “Begin the main function."
  • { and }: "This is where the main function begins and ends."
  • string name: (This is a variable declaration.)  "There is a variable in this function.   It is a string [text with multiple characters] variable, and we’ll call it 'name'."  The variable is the input, or whatever the user enters (this program is interactive with the user).
  • cout << "What is your name?  " << endl;: "Display 'What is your name?' with two extra spaces on the screen."
  • cin >> name; "Read what the user types on his or her keyboard.  Whatever they type before pressing Enter will become the value of the variable name (that's what name will mean)."
  • cout "It's very nice to meet you, " << name << "." << endl; "Display on the screen: 'It's very nice to meet you, ' then the value of the variable name, and then '.'"  This includes a space between "you," and the name that the user entered, as well a period at the end.

And you know what the rest means.  If all goes well, this is how the program should work (with the program output in green and my user input—what I type—in orange).

What is your name?  LadyPakenham
It's very nice to meet you, LadyPakenham.

The input (your name) is known in the program as the variable (or name).   The value of name is static, so whatever the user types in becomes name.  Then it’s placed wherever it is told to be placed.  Also, notice how cin (which, being the opposite of cout, is responsible for recording input rather than displaying output) will store whatever you type until you hit Enter.  With the exception of special characters, it'll store whatever you want.

In other words, you could type anything, which really opens the door for immaturity.

What is your name?  Fartface McFecalpants
It's very nice to meet you, Fartface McFecalpants.

The possibilities are endless.

A Couple Things to Note About cin

You knew this would be a problem: cout and cin are close to each other, but have their differences.  A big one to remember is that their signs are different:

cout << name;   //cout uses less-than signs
cin >> name;    //cin uses greater-than signs

I try to remember it this way: both signs kind of look like mouths talking.  cout kind of looks like he's doing all the talking, while cin looks like he's listening to the stuff in front of him talking.  Get it?  Output is like talking, recording input is like listening to somebody else talk.

There's something else to remember, too: unlike its companion, cin has a bit of a quirk to it.  Notice how, when using cout, you include an endl on every line (which, again, is like the program pressing the Enter key on a keyboard when displaying something onscreen).  You probably are going to want to have a line break and carriage return inserted after the user types a response, too (otherwise, that'd be one long line of input and output).  However, cin doesn't like it when you insert an endl on the same line as it.  So to say this in a program:

cin >> name << endl;

would be illegal.  It also looks kind of weird.  After all, cin records input, but it's cout's job to display things on screen like a line break/carriage return.  So, you have to output the endl on a second line and as a second statement, like this:

cout << name;
cout << endl;   //Don't forget: both statements end with semicolons!

Here's Another Program With Lots of Variables

You might be okay with typing Fartface McFecalpants and storing all that in one variable.  But in some cases, you don't need to.  The program will so kindly allow you to differentiate your first name from your last name.  Besides, who ever said you could only have one variable?

Source code For "NameGame.cpp":

#include <iostream> //tells compiler what cout and cin are
#include <string>   //tells compiler what string variables are

using namespace std;

int main()
string first;       //three variables,
string last;        // all pertaining to names.
string middle;

//Find the user's first name
cout << "What is your first name?  " << endl;
cin >> first;   //records what the user types
cout << endl;
//Find the user's last name
cout << "What is your last name?  " << endl;  
cin >> last;
cout << endl;

//Find the user's middle name
cout << "And your middle name?  " << endl;
cin >> middle;
cout << endl;
//Put it all together aaaand...
cout << "So your name is " << first << " " << middle << " " << last << "?"
     << endl;  //Had to break up a line there...
cout << "That's a funny name.  But it's not your fault." << endl;

return 0;

You see a lot of familiar faces here, but you’ll notice that I have three variables instead of one.  I assign each a name as they are entered—first, last, and middle.   Then, when it comes time to spit them back out, I put in variable in the order I want them to appear (again, including spaces for formatting).

So let's get back to our good ol' friend Fartface:
What is your first name?  Fartface
What is your last name?  McFecalpants
What is your middle name?  Philip
So your name is Fartface Philip McFecalpants?
That's a funny name.  But it's not your fault.

Indeed.  Note that if, in the source code, I had ordered the variables last, first, then middle, the output would have be McFecalpants Fartface Philip.  Which is just weird, right?

So, until next time, have fun writing programs where you can enter in whatever the hell you want.  And remember: you don't have to just write carbon copies of the examples I post!  When I was learning C++, I tried to think up of my own programs that used the same concepts but did different things.  It's a great way to really get familiar with the language, and practice is so important to becoming proficient in anything you do.  And remember that there's no limit to how many variables you can have, so have fun with them!  : )

No comments:

Post a Comment