Simple C++ question?
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();
}
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()
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();}
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).
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.
If you want to separate the two lines for readability (which is a good thing), you can write to cout twice:
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;}
Quote:Original post by jykQuote: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";
Quote:Original post by DenzinUnless 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.Quote:Original post by jykThat is true, but I personally believe its a good habit to get into.
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).
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]
Quote:Original post by DenzinFair enough, but why?
That is true, but I personally believe its a good habit to get into.
Quote:Actually still only right it once and just seperate with whitespaceWhat's wrong with repeating cout on the second line?cout << "Line one...\n" << "Line two\n";
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";
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement