in the first part of the series I'm writing we made a program that displays "Hello world" to the screen.
in this part we will go further into the BASIC use of the iostream class (iostream inherits all members from istream and ostream{input and output streams} so instead of including both istream and ostream we can just include iostream.
OK, lets use this as our blank from dev c++
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}
before we just printed something to the screen using the ostream part of the iostream class, now lets look at getting input to our application, in this case we will be taking input from the keyboard and sending it to the screen using cin.
we use cout (look at it, c-out) for output and now cin (c-in) for input, simple enough not to mix em up.
ok, first we need to declare a variable, well first we need to think at what we want, do we want to input numbers, characters,files,string or one of the many other data types? lets try string, so we need to declare our variable as a string.
there are many many ways to declare a variable and initiliaze it we wont worry about the many ways to do something because this is a basic tutorial remember?
the way i set up variables is i define what data type it is (in this case its a string) and then the variable name, in this case we are going to call the variable stuff. you can call it whatever you please (with the exception of pre-defined variable names, but again it a basic tutorial. if you want to learn all that good stuff attend a school.
i define all my variables below "using namespace std;" and above "int main(int argc, char *argv[])" just because thats how i do it.
our code to define the variable stuff to use the data type string will be
string stuff;
i could have just told you to copy that in, but i want you to know why we are doing what we are doing.
OK, so this is what my code is looking like.
#include <cstdlib>
#include <iostream>
using namespace std;
string stuff;
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}
ok, now we are going back into main (between the { and the }),now we need to get input to that variable using the keyboard, this is where we use cin.
remember when i said the double carrots (<< >>) define which way the information is sent? we need to take from the keyboard and send to the variable.
cin >> stuff;
think of cin as the keyboard for now, so the carrots are pointing to stuff from the keyboard.
now we want to display on the screen what we typed in to make sure its working.
cout<<stuff;
that will send stuff to the screen
there is something called endl; (end line), that way it breaks a line instead of saying "press enter to continue" on the same line stuff is being displayed on. think of endl; as the return key in a text file.
the finished code
#include <cstdlib>
#include <iostream>
using namespace std;
string stuff;
int main(int argc, char *argv[])
{
cin >> stuff;
cout<<stuff<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
-Symantic