Advertisement

C++ questions

Started by March 24, 2002 02:40 PM
6 comments, last by J e s t e r 22 years, 8 months ago
Im reading C++ for dummies and have some questions that the book doesnt cover. for (; { some code... With (; does this mean that the code for the for is on the next couple of lines? int value = 0; cout << "Enter something: "; cin >> value What does cin mean? (Stand for) If I understand correctly it places the value of the user input into the variable "value" right? << and >> Again if I understand correctly: << output >> input right? And one last thing for now. Whats the diffrence between cout and printf? I see printf in C alot, but have yet to see it used in C++. Is cout just a new form of printf for use in C++? I wish this book would explain more about the code then it does. Lucky for me its rather easy to understand. I think they should change the books title to: C++ for dummies with prior knowledge of C, or the ability to read the authors mind. 12/32/84
12/32/84
for( ;; )
{

}
that loops the code in the brackets for ever.

for( ;; )
that loops the next line for ever.

cin stands for "c in" and cout means "c out".

as for the cout << and cin >>, yea, but they are really bit manipulators (sp), but you can ignore that for now.

cout is basically the C++ version of printf.

Will O''Connor, Flamma Productions.
[email=webwill666@hotmail.com]Will O'Connor[/email], Flamma Productions.
Advertisement
Actually, cin refers to ''character input stream'' (as opposed to wcin, the wide character input stream).
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
The for construct takes up to three parameters, all of which are optional and are separated by semicolon:
for (initialization ; loop_condition ; control )

Often, the first part is for initializing a counter variable. The second bit specifies the condition or conditions required for the loop to continue running. The last part is often used to increment a counter variable, but can be used for just about anything else. It's executed when the loop hits the bottom and is about to go the top for the next iteration. Truthfully, those three sections can be used for whatever you want but, apart from eliminating a few, it's best to stick with the conventions.

You can omit any one of these parameters, providing you keep the semicolons. So, let's say that you have a variable i already initialized to 0, you want the loop to continue while i is less than 10, and you need i incremented by 1 for each iteration. It'd look like this:

for( ; i < 10; ++i)
{
// stuff
}


You get the idea now, I'm sure.

[edited by - merlin9x9 on March 24, 2002 4:02:28 PM]
The first expression in the for loop is executed when the loop starts, and it doesn't matter whether anything is there or not. The loop will iterate until the second expression is false, so if there is nothing there it will loop forever (until you 'break' it). The last expression is executed after every iteration, and if nothing is there then the loop will just continue without doing anything.

<< and >> are overloaded operators (they are normally bit shifts) and in this case, yes, << is used for output and >> for input.

cout is basically just an improved version of printf. It is much easier to use in almost all cases and does pretty much the same thing as printf.

[edited by - SilentCoder on March 24, 2002 4:02:05 PM]
In the context of stream I/O:

(overloaded) << is the insertion operator
(overloaded) >> is the extraction operator
Advertisement
Thanks everyone.

It all makes more sense now.

12/32/84
12/32/84
I also bought the C++ for Dummies book. I am enjoying it quite a bit. However, there are waaay too many errors. Luckily I caught most of them, but since I am no C++ genius I still find myself questioning whether I''m right or the book is right.

After I''ve finished C++ and have gotten used to it I''m going to move onto graphics programming; probably OpenGL.

Massif - "It means mountain."
Massif - "It means mountain."

This topic is closed to new replies.

Advertisement