tags:

views:

112

answers:

2

Possible Duplicate:
Difference between void main and int main?

Alright, so I'm using bloodshed complier, first thing I should note is I'm using a 2001 edition C++ for the absolute beginner, was there any changes to C++ since 2001 that would effect the validity of this book? I ask because I know php has php5 and now php6 but I haven't seen any C++03.

Now for the reason I asked that, in this code it uses,

void main (void)

why would I want an argument to be void? what does this do.

But when I run the void main (void) in my compiler it says that main must have a "int" before it. So I can't have "void" main (void);

Also, once the code runs through, as long as there isn't a "cin" the program closes. Is this normal behavior, is there a better way to stop this besides making a cin at the end of my program?

+8  A: 

Burn that book. Not only is void main not conformant to any C++ standard (modern or old), but declaring a parameter-less function with (void) instead of () is a C idiom that is discouraged in C++. From this sample, I shudder to think what other horrors that book might contain.

The issue with the console window closing is completely unrelated to the contents of your program. Windows has a behavior where if you launch a console program directly, it will automatically close the console window when the program ends. To avoid this, open a console window yourself, and run your program from within it. The console window will stay open as long as you want it to. (And please refrain from using the unfortunately popular practice of adding an extra dummy input or a "pause" call at the end of your program to keep the window open. You should not be adding code to your program to compensate for a particular way that you happen to be launching it; just launch it the right way and leave your poor code alone.)

Tyler McHenry
Horror vacui? ;-)
Péter Török
well the code complies so far, of course from what i understand OOP is in itself another whole book so far crammed into a chapter. generally just want to know if C++ took any changes since 2001 that would have made this book's information obsloete. the PHP% edition was a joke considering the writer never used sessions. but the books are still good for sinking teeth.
TimothyTech
What I'm trying to say is that it doesn't matter whether there have been changes to C++ since 2001 (there have been some), because from the sample you showed, the book is not accurate even with respect to the 1998 C++ standard. This makes it obviously a very bad book to learn with.
Tyler McHenry
A: 
Owen S.
well how it works is if i put cin then the program pauses waiting for my response before it finishes the program and closes. so i stick a cin at the end of my program so it pauses and lets me review the outcome. otherwise the program closes too fast for me to even read one line. thanks for the reply. makes alot of since. so i should be okay with a 2001 book?
TimothyTech
@Timothy Did you read the second part of my answer which address the question of why your program is closing and how you can prevent it? And there's nothing wrong with using a 2001 book, but for your own sake, use a *different* 2001 book that is at least accurate to the standards that existed when it was published.
Tyler McHenry
It is legal to leave the return statement out of `main()`; it will implicitly return zero at the end. But you certainly do have to declare an `int` return type, and if the book says otherwise then it's wrong.
Mike Seymour
@TimothyTech If the book is readable and at the right level for you, I'd depart from the general opinion of my colleagues and say go ahead and use it - the book was wrong to write it this way, but I think this is a fairly minor gotcha. Like any book, read critically, and if something doesn't make sense or doesn't work, just ask. And remember, there are plenty of other C++ books in the sea. :-)
Owen S.
yes sir, but its the best free copy i have found. you know you any free copies that are better please let me know. im on chapter 5 and this is the only issue ive encoutered. the other book i had was using "std::cout <<" and that seems obsolete to me.
TimothyTech
Nope, `std::cout << "Hello, world"` is the currently correct way to print text. What does your book recommend? In particular, is it so old that it assumes #include <iostream**.h**> ?
MSalters
using namespace std; is std::cout really the current way?
TimothyTech
No. Either you write "std::cout" or you write "cout" and make sure you have a "using namespace std;" somewhere above. Doing both is needless wear and tear. :-)
Owen S.
yeah the book said use "using namespace std;" std::cout seemed tedious. lol man your good.
TimothyTech
Free is hard to beat, but I see some cheap Lippman 3rd eds. on eBay. Many local bookshops will dump older tech books for peanuts too.
Owen S.