Advertisement Jump to content
Sign in to follow this  

Need a few things clear... :)

This topic is 4903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First of all can someone explain to me what these mean: << cin And please take a look at this code: // Guess My Number // The classic number guessing game #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); // seed random number generator int theNumber = rand() % 100 + 1; // random number between 1 and 100 int tries = 0, guess; cout << "\tWelcome to Guess My Number\n\n"; do { cout << "Enter a guess: "; cin >> guess; ++tries; if (guess > theNumber) cout << "Too high!\n\n"; if (guess < theNumber) cout << "Too low!\n\n"; } while (guess != theNumber); cout << "\nThat's it! You got it in " << tries << " guesses!\n"; return 0; } Does it mean that if '(guess != theNumber);' is true, then it repeats the code. But if its false then it executes: 'cout << "\nThat's it! You got it in " << tries << " guesses!\n";'. This code has confused me :( Thanks, Final

Share this post

Link to post
Share on other sites
<< is a redirection pipe. Meaning that everything to the right of it will be shoved to the left ( hence they are pointing to the left ).

cout is the standard out. The standard output stream of your program. Typically the console window.

Hence, cout << "Bananaboat", means "Move the string "bananaboat" to the console".

Yes, the do .. while. Means that everything inbetween the {} should be executed as long as the while statement is true and when its false, continue.

Share this post

Link to post
Share on other sites
Original post by FinalShot

Tricky. The best answer to begin with, is that cin is a mysterious black box that reads stuff from the keyboard, and dumps it into the variables you put after the <<'s. :)

Once you're starting to feel comfortable with C++, the answer gets a bit more complex. cin is a stream, and << is known as the extraction operator (because it extracts data from the stream).
Streams are really just an abstraction for reading and writing to/from... stuff... Keyboard, the console window on the screen, a network connection, a file, or even a memory buffer. cin and cout are just predefined streams. And << is used for extracting data from whatever source the stream is reading from. In the case of cin, it's "standard in", which is usually connected to the keyboard. It can be redirected though, for example to take its input from another program's output, or a text file.

Dunno how much sense this makes to you. Feel free to stick with the first explanation. [smile]


Does it mean that if '(guess != theNumber);' is true, then it repeats the code. But if its false then it executes: 'cout << "\nThat's it! You got it in " << tries << " guesses!\n";'.

Yep. Whatever is inside the parenthesis have to be true for the loop to continue. So in your case, guess has to be different from theNumber. Once that's false, it exits the loop, and executes the last few lines in the program.

Oops, just realized you were talking about <<, not >>
All the above applies to >>, which is used to extract data from the cin stream (or any other input stream).
<< is the opposite, and is used to insert stuff into a stream (and so that works with output streams like cout, instead of input streams like cin).
Cout is a stream that's by default connected to the console window. So when you insert data into that stream, it gets printed on the console.

Share this post

Link to post
Share on other sites
Although I know this will complicate the answer, << and >> are also bit shift operators (left and right respectively). The above posts pertain to their use with cout and cin (and similar input/output mechanisms). The operators perform completely different actions when used with integers. For example,

unsigned int Value=4; /* 4 = 0000 0100 */
unsigned int Shift=2;

Value = Value << Shift; /* 16 = 0001 0000 */

Value <<= Shift; /* 64 = 0100 0000 */

(taken from Bitwise operations). See also, Bitwise operation, and Bitwise Operations in C.

Share this post

Link to post
Share on other sites
<< and >> are binary operators, which is to say, the same "kind of thing" that + (addition) and binary * (multiplication) are.

In C++, it is possible to make user-defined types of things, and write code that describes how an operator should work given the types of the things it's operating on.

"cin" is a global variable, provided by code that you include via <iostream>, which is of type "istream". Similarly, "cout" is a global variable in the same place of type "ostream". These two types represent 'streams' of data; these are like queues of bytes, more or less. There are sub-types of istreams and ostreams that can work with a file (either reading from it or writing to it), or from a string in memory (treating the data in the string as if it were file data); the cin and cout objects treat the console like a stream.

Finally, there are definitions for operator<< that take an ostream instance on the left side, and various types of things (basically, the primitive types like int, float, etc., and a couple of others) on the right side, and are implemented to mean "append a textual representation of the right side to the end of the stream on the left side; then return the object on the left side" (yes, "return"; the operator implementation is really a function implementation.

The net effect is that for example, cout << "hi there" << endl gets translated into:

operator<<(operator<<(cout, "hi there"), endl);

Now, the "operator<<" there is really just a function name, so let's replace that with something friendlier to aid our understanding:

printTo(printTo(cout, "hi there"), endl);

The effect of the inner call is to put the text "hi there" onto the stream that the cout object represents, i.e. the console. Thus we put that text into the console stream. Then the return value is the stream object, i.e. cout.

// "hi there" gets sent to console
// then we call:
printTo(cout, endl);

Then the same thing happens, to put the endl object onto the stream. This does some magic; the effect is that a newline (return character) is put onto the stream, and then the stream is also told to "flush", making that text actually appear in the console window.

// "hi there" gets sent to console
// '\n' gets sent to console
// console is told to display what it received
// and we are left with just the 'cout' object as a result,
// but that gets ignored because we don't assign it anywhere.


As for your while loop, you basically have it correct. A do-while loop as illustrated runs at least once, and at the end of each loop, it will go back if the condition is satisfied. So we have

1. Run the loop.
2. If guess doesn't equal the number, go to step 1.
3. If we got here, guess must equal the number (otherwise we would have looped).
Therefore, tell the user s/he won.

Share this post

Link to post
Share on other sites
One of the interesting things about << and >> is that their names change (when speaking aloud) depending on how they are used. If you use them to shift bits they're called bit shift operators or shift left and shift right. If you use them on streams << is the insertion operator and >> is the extraction operator.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!