=More on cin and cout=
cin and cout are the most commonly used for input and output in c++. Although there are other methods of inputting stuff to variables, but let’s just stick with cin and cout since they are the easiest.
First of all, if you want to use cin and cout, you must first include the header file iostream(#include < iostream >). < > brackets tells the compiler to look in the default directory for the header file and “” is used to tell the compiler to look for the header file in the folder where the cpp file is stored.
Before you can use cin and cout, you have to put this statement “using namespace std;” into the cpp file after #include < iostream > so you don’t have to use the prefix std:: before using cin, cout, endl, ect. Remember, orders counts. Because the compiler doesn’t know what the namespace std, because you didn’t tell the compiler to look into the iostream header file. You must tell it where to look before you can tell the compiler to do something.
Ok yea, reading is boring, I know. So let’s get to an example.
#include < iostream >
using namespace std;
int main()
{
int a;
cout << “Please enter a number: “; //this is known as prompt line
cin >> a;
cout << endl;
cout << “You have entered: “ << a; // outputs variable a
system(“pause”);
return 0;
}
You might be wondering how do you remember that the << sign points to left if you want to output a string, and >> points to right for inputting. It is easy to be confused I know. I remember my first program, I got confused over where those things should point to and I got a bunch of errors and took me like days to figure out what is wrong with the thing.
This is how I remember where that thing should point to. If you want to output a string, you throw the string, which is what is between the “”s, to the output cout. For input, you throw whatever the user had entered to a variable, which in this case, a.
With cin, you can also use it to throw 2 or more user input to 2 or more variables. It’s going to look like this cin >> a >> b >> c; you can always add more variables in, but I don’t recommend you to do this, because the user just might enter it wrong. Also, keep this in mind, >> skips whitespaces. DOCX File - http://www.mediafire.com/?menz1xjkfdn
cin and cout are the most commonly used for input and output in c++. Although there are other methods of inputting stuff to variables, but let’s just stick with cin and cout since they are the easiest.
First of all, if you want to use cin and cout, you must first include the header file iostream(#include < iostream >). < > brackets tells the compiler to look in the default directory for the header file and “” is used to tell the compiler to look for the header file in the folder where the cpp file is stored.
Before you can use cin and cout, you have to put this statement “using namespace std;” into the cpp file after #include < iostream > so you don’t have to use the prefix std:: before using cin, cout, endl, ect. Remember, orders counts. Because the compiler doesn’t know what the namespace std, because you didn’t tell the compiler to look into the iostream header file. You must tell it where to look before you can tell the compiler to do something.
Ok yea, reading is boring, I know. So let’s get to an example.
#include < iostream >
using namespace std;
int main()
{
int a;
cout << “Please enter a number: “; //this is known as prompt line
cin >> a;
cout << endl;
cout << “You have entered: “ << a; // outputs variable a
system(“pause”);
return 0;
}
You might be wondering how do you remember that the << sign points to left if you want to output a string, and >> points to right for inputting. It is easy to be confused I know. I remember my first program, I got confused over where those things should point to and I got a bunch of errors and took me like days to figure out what is wrong with the thing.
This is how I remember where that thing should point to. If you want to output a string, you throw the string, which is what is between the “”s, to the output cout. For input, you throw whatever the user had entered to a variable, which in this case, a.
With cin, you can also use it to throw 2 or more user input to 2 or more variables. It’s going to look like this cin >> a >> b >> c; you can always add more variables in, but I don’t recommend you to do this, because the user just might enter it wrong. Also, keep this in mind, >> skips whitespaces. DOCX File - http://www.mediafire.com/?menz1xjkfdn