April 21st, 2006 05:56 PM
Simple C++ question!
std::cout << "Enter two numbers:" << std::endl;
std::cin >> v1 >> v2;
Well, I don't understand how does the ">>" or "<<" works in example metion aboved. Can anyone explain?
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.
April 21st, 2006 07:49 PM
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)