Simple C++ question!
Results 1 to 2 of 2

Thread: Simple C++ question!

  1. #1
    Senior Member
    Join Date
    May 2003
    Posts
    226

    Simple C++ question!

    Code:
    std::cout << "Enter two numbers:" <<  std::endl;
    std::cin >> v1 >> v2;
    IN C++ every expression produces a result, which typically is the value generated by applying an operator to its operands. In the case of the output operator, the result is the value of its left-hand operans. That is, the value returned by an output operation is the output stream itself.
    Well, I don't understand how does the ">>" or "<<" works in example metion aboved. Can anyone explain?

  2. #2
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    You normally won't see it written like that...

    Usually more along the lines of :

    cout << "Enter two numbers\n";
    cin >> v1 >> v2;
    cout << "Numbers are " << v1 << " and " << v2 << endl;

    For output, basically just think of it like concatentation of different things, so in the 3rd line it would be a concatenation of the static string "Numbers are", the value of v1, " and ", the value of v2, and an end of line character (\n or using cout endl). The equivalent in c would be along the lines of :
    printf("Numbers are %d and %d\n", v1, v2);

    For the input, basically think of the >> operator as a 'get input from stdin and assign to the variable on the right hand side. With more than one >> operator, it'll just take more input (wanting to say on different line, ie, \n is the seperator in input, but don't remember, haven't used C in a logn time) and assign it to the next variable and so on and so forth.

    So, cin >> v1 >> v2, functionally, is no different than cin >> v1; cin >> v2;
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides