Results 1 to 5 of 5

Thread: Cin, Cout. can someone help a newb.

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    New Hampshire
    Posts
    32

    Post

    I want to know how to use Cin and Cout Correctly.

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {   cout << "Some output text"  //send a string to the console
             << endl; // send a newline character
    
        int a;
        cout << "Enter an integer: "; //prompt user for input
    
        cin >> a; //get user input and put it in variable a
    
        cout << endl << endl //go down a couple of lines
             << "You entered the integer "  //output a string
             << a   //output an integer
             << endl;  //another newline
    
        return 0; //end function
    }
    That's basically how it works. Nothing fancy.

    << is the stream insertion operator
    >> is the stream extraction operator

    cin and cout are streams. cin is the input stream, and cout is the output stream. You insert things into the output stream and extract them from the input stream. Makes sense, really
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    You may encounter problems using cin with multi-word input. In that case you use cin.getline
    Code:
    char text[256];
    cout<<"Enter Sentence"<<endl;
    cin.getline(text,256);
    cout<<text<<endl;
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    you could also use
    printf to print text out(like cout)
    scanf - to take input from the keyboard.
    AS far as i see they are the same as cout and cin but i found these easier to use
    Matt

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    'cout' and 'cin' are easier to use than 'printf' and 'scanf'. 'printf' and 'scanf' have more complex sintax, if you want to display a value have to use %d or %s or..
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width