Jump to content
  • Advertisement
Sign in to follow this  
samurai_gator

Simple C++ question?

This topic is 3300 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'm getting these messages when trying to compile the following program: C:\Users\josh\Basic Functions\main.cpp|16|error: `main' must return `int'| C:\Users\josh\Basic Functions\main.cpp|16|error: return type for `main' changed to `int'| ||=== Build finished: 2 errors, 0 warnings ===| Here's the source. Any suggestions? #include <iostream> using namespace std; void R(); void R() { cout << "You are learning new stuff!\n" << "Don't stop!\n" << endl; } void main() { R(); }

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by samurai_gator
C:\Users\josh\Basic Functions\main.cpp|16|error: `main' must return `int'|
C:\Users\josh\Basic Functions\main.cpp|16|error: return type for `main' changed to `int'|

How much clearer can an error message be? You must change this line:

void main()

to this:

int main()

Share this post


Link to post
Share on other sites
A couple of tips:

// Use [ source ] tags (no spaces) to put your code in one
// of these nice little boxes :)

#include <iostream>
using namespace std;

// There's no need to declare R() here (when you learn more about declarations
// and what they're for, it'll become clear why this is).
//void R();
void R()
{
cout << "You are learning new stuff!\n" << "Don't stop!\n" << endl;
}

// Hint: look up the valid function signatures for main() in standard-
// conforming C++.
void main()
{
R();
}

Share this post


Link to post
Share on other sites
Quote:
use int main and return an int at the end of your main code block.
Provided the compiler being used complies with the standard in this respect, it's actually not necessary to include an explicit return statement in main() (in the absence of such a statement, zero will be returned).

Share this post


Link to post
Share on other sites
Another tip: std::cout doesn't require that it be fed individual lines to operator<<(). You can feed two lines at once. Also, std::endl is basically a newline followed by a call to flush() the stream, which means that any data that had been buffered will be written to the screen.

So when you wrote "...\n" << endl, you are actually writing two newlines in succession. Maybe you intended that, but if not you can omit the extra \n.

void R()
{
cout << "You are learning new stuff!\nDon't stop!" << endl;
}



If you want to separate the two lines for readability (which is a good thing), you can write to cout twice:

[void R()
{
cout << "You are learning new stuff!\n";
cout << "Don't stop!" << endl;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
use int main and return an int at the end of your main code block.
Provided the compiler being used complies with the standard in this respect, it's actually not necessary to include an explicit return statement in main() (in the absence of such a statement, zero will be returned).


That is true, but I personally believe its a good habit to get into.

Quote:
Original post by rip-off
lines for readability (which is a good thing), you can write to cout twice:


Actually still only right it once and just seperate with whitespace

cout << "Line one...\n"
<< "Line two\n";




Share this post


Link to post
Share on other sites
Quote:
Original post by Denzin
Quote:
Original post by jyk
Provided the compiler being used complies with the standard in this respect, it's actually not necessary to include an explicit return statement in main() (in the absence of such a statement, zero will be returned).
That is true, but I personally believe its a good habit to get into.
Unless you are writing a comand line tool which will be required to conform to the POSIX standard, the chance that you will ever need to return anything other than zero from main() are pretty slight.

And since program return values aren't guaranteed to be portable beyond POSIX-certified systems (i.e. Solaris and Mac OS X), I wouldn't advise you use return values for meaningful communication anyway [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by Denzin
That is true, but I personally believe its a good habit to get into.
Fair enough, but why?

Quote:
Actually still only right it once and just seperate with whitespace
cout << "Line one...\n"
<< "Line two\n";
What's wrong with repeating cout on the second line?
If you're trying to minimise on clutter and noise then why not leave out the extra streaming operator too?:
cout << "line one...\n"
"line two\n";

Share this post


Link to post
Share on other sites
Quote:
That is true, but I personally believe its a good habit to get into.
I'm also curious as to why you think returning zero explicitly at the end of main() is a good habit (I'm not saying it isn't - I'm just curious as to your reasons).

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!