L33T 5K00L

Would you like to react to this message? Create an account in a few clicks or log in to continue.

Talk about video games, put up incredible polls, and go insane


2 posters

    More on Cin and Cout part 1

    Poll

    Did this helped?

    [ 2 ]
    More on Cin and Cout part 1 I_vote_lcap67%More on Cin and Cout part 1 I_vote_rcap [67%] 
    [ 1 ]
    More on Cin and Cout part 1 I_vote_lcap33%More on Cin and Cout part 1 I_vote_rcap [33%] 

    Total Votes: 3
    GroupZero
    GroupZero
    Admin
    Admin


    Male
    Number of posts : 572
    Age : 29
    Description : I'm You!
    Reputation : 11869
    Registration date : 2009-02-25

    More on Cin and Cout part 1 Empty More on Cin and Cout part 1

    Post  GroupZero Tue Oct 13, 2009 5:06 pm

    =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
    Goreblood
    Goreblood
    Admin
    Admin


    Male
    Number of posts : 1313
    Age : 28
    Description : (-)(|\)(|||)(\/)(///)(//)(-)(|\/|)(-|-)(|\|)(-|-)(///)(|\|)
    Reputation : 12626
    Registration date : 2009-02-25

    More on Cin and Cout part 1 Empty Re: More on Cin and Cout part 1

    Post  Goreblood Thu Oct 22, 2009 9:03 pm

    Ahhh nicely described and has more than i said in the basics. Nice. LOL

      Current date/time is Fri Apr 26, 2024 10:53 pm