Site hosted by Angelfire.com: Build your free website today!

 

Welcome to the third chapter !

This chapter is all about getting input from the user of your program. If you haven't done so already, read the first two chapters as they are necessary to understand this one.

To get input we use another C++ object : cin
This has three useful versions, the basic cin, cin.get and cin.getline

In this chapter I will talk about the basic cin, this will take data from a device ( here your screen ) and store it in a variable. Here's a little example:

                                               MyInt;
                                               cin>>MyInt;

Say the user typed a '1' , this value would then be stored in MyInt and can be used later with cout

                                               cout<<MyInt;

This would input 1 to the screen.
You may have noticed that after cin there is the >> sign rather than the << sign. This means that the data is flowing in rather than out. Don't try using the << sign with cin though, it won't work.

Now for a little example program:

#include <iostream>

using namespace std;

int main()

{

        int MyInt;

        int MySecondInt;

       cout<<"Please enter a number"<<endl;

       cin>>MyInt;

       cout<<"Please enter another number"<<endl;

       cin>>MySecondInt; 

       MyInt += MySecondInt;

       cout<<"number 1 + number 2 = "<<MyInt<<endl;

return 0;

}

 Nothing very complicated here, this line may have puzzled you though:

                                               MyInt += MySecondInt;

This is the same as 

                                               MyInt = MyInt + MySecondInt;

but faster to write. You can also do -=, *=, /= and %=
Thats about all there is to say about basic input although there's lots more to learn about it. Next chapter will be about arrays and loops. 

Thanks for reading

                         PenTen

( any suggestions, questions or insults :) please send them here )

 

 

 



 

NEXT TUTORIAL