Sign in to follow this  

Correct C++ Programming

This topic is 4836 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

I have a C++ book that is dated (1998, but still very useful, from what i've been reading) and it shows a certain program: #include <iostream> using namespace std; int main() { cout << “Every age has a language of its own\n”; return 0; } But, another source that is online (gametutorials.com) shows this program instead: #include <iostream> using namespace std; void main() { cout << "Every age has a language of its own!" << endl; } Using VisualC++ 6, the first one doesn't even work, but the second one does! I don't understand why having something that is easier to type would work over something that is harder and actually in a book- I mean, it says its ANSI standard, so it should work... My question is, is it detrimental to use it the second way? Because if it isn't, i'll just have to block out whenever the book uses things that way and use it my own way. Thanks in advance!

Share this post


Link to post
Share on other sites
the 1st one doesn't work because u miss typed the 1st quote it's always "sometext" not “sometext" (look well the 1st quote is backward in u're 1st example) as for wich is right the 1st one is right (once u fix the quote) the 2nd is ok to but non standard (void main isn't supported by all compilers use int main) the endl part or \n is a matter of choice tho i think it works everywhere

Share this post


Link to post
Share on other sites
You should always use endl when ever possible. endl flushes the output buffer as well as starting a new line. At the same time \n only starts the new line. Alternitvly you can do
cout << "This is a combo of both!\n" << flush;

Share this post


Link to post
Share on other sites
As ranakor points out, the second example is not standard compliant. To quote the standard:

Quote:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }

and
int main(int argc, char* argv[]) { /* ... */ }



Ergo, any form of main with a return type other than int is illegal.

Note that it is not necessary to explicitly return 0 from main. If no value is explicitly return, a value of 0 (signifying successful completion) is returned for you.

Note on endl: This is not equivalent to '\n'. endl inserts a line termination sequence (whether that is, on your platform, LF, CR, or CRLF) and flushes the stream.

Share this post


Link to post
Share on other sites
what they are saying most simplistically, zero, is that you should never use a "void" main() function. it goes against ANSI standards and is just plain bad practice. theres no reason the first shouldn't work....if it really gives you an error you must have typed something wrong in the code but not onlline. try copy+pasting the following code, it's the exact same thing:

//Hello World.cpp
#include <iostream>
using namespace std;

int main()
{
cout << "Hello world!" << endl;
return 0;
}




hope this helps :/

Share this post


Link to post
Share on other sites
As have been said, there nothing wrong about the first example, except the typo.

As for the other example, the only thing that's not ANSI there is the use of void for main. However, not returning something from main is ok.

Edit: I see now that this has already been said here...

Share this post


Link to post
Share on other sites
You're using buffered output, so when you do something like
cout << "hiya";
"hiya" is added to a buffer, but it isn't normally outputted to the screen. When you flush the buffer it is outputted and the buffer made empty. This happens if you tell it to flush, or if the buffer gets full or sometimes because of something else (you never know).

Share this post


Link to post
Share on other sites

This topic is 4836 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this