Are YOU talking to ME !
Home Page Parent Page Manipulating an output stream

 

Are YOU talking to ME !?!?!

 

Now we get to actually give the computer some information. Up to now, everything was rather static. The program couldn't really do anything useful. Now we can give it information at run-time.

The new command is cin. Pronounce it like you are a minister, S-I-N as in SINNER. Okay, getting back to our theme. Yo, now you can actually give your programs some information WHILE it's runnin'.

Okay, cin is used a lot like cout in that you go cin>>variable name. I usually think of the >> as Pac-Men (tm). Remember the >> are trying to eat cin. Remember this, because I can't even begin to tell you how many headaches I have gotten from forgetting this.

Want an example?

#include <iostream.h> //remember to take out the spaces in between the < >.

 

void main() {

               int x, y; //some integer variables

               long int a;

               cout << "Please enter a number ";

               cin >> x;

               cout << "Please enter another number ";

               cin >> y;

               a = x * y; // I use a long just in case the result
                          // of x * y is larger than 32,767

               cout << "The result of " << x << "times" << y << "is" << a;
                            //spit out the result

               cout <<"Press "E", then enter to end the program"<<endl; 
                      //pause program execution so that we can see the results.

               cin >> a; // the program will wait for the user to 
                 //enter a character. Once the character is read, the program ends.

} //end of main function

 

See, easy. But there is something I really need to start mentioning. I have not told you about the manipulation characteristics of cin and cout. That's right, you can manipulate how many spaces are displayed and whether a variable's contents will be displayed in decimal or hexadecimal. This is going to take up two tutorials, so I am going to have to change things around.

1