0

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?

Community
  • 1
  • 1
TimothyTech
  • 745
  • 2
  • 11
  • 18

2 Answers2

8

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
  • 74,820
  • 18
  • 121
  • 166
  • 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 Jun 09 '10 at 20:57
  • 7
    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 Jun 09 '10 at 20:59
0

What there have been since 2001 are changes to C++ compilers, which on the whole are much pickier about how standards-conformant your C++ must be before they accept it. For example, gcc has gone from 2.95 or so all the way to 4.4.2. Earlier compilers may have let you slide with this form of main, as they would several old C-isms that are not proper C++, but it's not the language that's changed in this case (at least, as far as the standard goes).

Having said that: yes, C++ has changed, and is changing, though not quite yet at the official standards level. Different compilers support those changes to different extents. I don't think you're likely to be impacted by them, but you may want to be aware of them at any rate. See:

As far as how to end your program. I recommend a simple "return 0;" at the point in main where you want to exit – or use an exit code other than 0 if you want to exit with an error. You can omit the return, though, and many examples do this, though I personally think it's bad style.

I understand you're using cin to pause the application before it exits. This isn't required for C++ apps in general, it's just a convenience for running the app in the manner you are. It's OK for examples, but you'd definitely want to avoid that for a real application.

Owen S.
  • 7,665
  • 1
  • 28
  • 44
  • 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 Jun 09 '10 at 22:01
  • @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 Jun 09 '10 at 22:10
  • 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 Jun 09 '10 at 22:35
  • @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. Jun 09 '10 at 23:25
  • 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 Jun 09 '10 at 23:29
  • 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 ? – MSalters Jun 10 '10 at 09:04
  • using namespace std; is std::cout really the current way? – TimothyTech Jun 10 '10 at 15:51
  • 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. Jun 10 '10 at 16:19
  • yeah the book said use "using namespace std;" std::cout seemed tedious. lol man your good. – TimothyTech Jun 10 '10 at 16:33
  • 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. Jun 10 '10 at 16:43