[QUOTE=Leonmyster;32304376]I just have a general question about the return statement for C++.
Why do we always need to have
[B]return 0;[/B]
Is it bad if I forget to include it at the end of the main?[/QUOTE]
Not many operating systems rely on it, but it's good practice.
This is only regarding the int main though.
[QUOTE=Leonmyster;32304376]I just have a general question about the return statement for C++.
Why do we always need to have
[B]return 0;[/B]
Is it bad if I forget to include it at the end of the main?[/QUOTE]
If you don't put it in it will default to return 0 (only true for main()). If it didn't you would get an error saying that main() has to return something.
iirc you don't need it in the main-method. If you have no return-statement at all in there, return 0; is implied [citation needed].
Doing that in other functions causes undefined behavior though.
One use of that is so you can check in scripts if the program ran (un)successfully and proceed accordingly.
[QUOTE=Leonmyster;32304376]I just have a general question about the return statement for C++.
Why do we always need to have
[B]return 0;[/B]
Is it bad if I forget to include it at the end of the main?[/QUOTE]
Your compiler should bitch and/or moan if your main function is (a function with a return type that isn't int|a function that says it returns int but never returns).
You should return 0 as any non-zero return value should be handled as your OS as meaning "this application didn't close quite the way it should have".
Is there anything against doing
[img]http://gabrielecirulli.com/p/20110915-213046.png[/img]
instead of
[img]http://gabrielecirulli.com/p/20110915-213143.png[/img]
in class header files? I think actually leaving the variable name would help the client programmer more by letting them know what it's supposed to be.
[QUOTE=TerabyteS_;32306612]Is there anything against doing
[img]http://gabrielecirulli.com/p/20110915-213046.png[/img]
instead of
[img]http://gabrielecirulli.com/p/20110915-213143.png[/img]
in class header files? I think actually leaving the variable name would help the client programmer more by letting them know what it's supposed to be.[/QUOTE]
Doesnt matter, I preffer having the variable name but its just a matter of opinion
It's only necessary if the name provides insight into whats going on. In the case you posted, the method name clearly documents what's happening.
Then again, the names that are obvious also aren't the ones you change very often, so how much time are you really saving?
[QUOTE=RyanDv3;32306681]It's only necessary if the name provides insight into whats going on. In the case you posted, the method name clearly documents what's happening.
Then again, the names that are obvious also aren't the ones you change very often, so how much time are you really saving?[/QUOTE]
Though I find it important to have consistency. Either never specify names or always do.
RyanDv3 makes a good point, but I prefer to name them all anyway purely for consistency's sake.
[editline]dammit[/editline]
Ninja
All right then, I think I'm going to name all of them from now on to save myself from possible headaches.
[editline]15th September 2011[/editline]
Oh, another question:
do I really have to specify things like
[cpp]using std::cout;
using std::cin;
using std::endl;[/cpp]
one by one, or can I just do
[cpp]using namespace std;[/cpp]
?
Will the latter bloat my program?
[QUOTE=TerabyteS_;32306809]All right then, I think I'm going to name all of them from now on to save myself from possible headaches.
[editline]15th September 2011[/editline]
Oh, another question:
do I really have to specify things like
using std::cout;using std::cin;using std::endl;
one by one, or can I just do
using namespace std;
?
Will the latter bloat my program?[/QUOTE]
Well, the latter brings everything from inside the std namespace into your global namespace, so name collisions and stuff are more likely to happen. You can just use std::cout << "whatever" << std::endl; like that anyway, it's not much extra typing to be honest.
[editline]15th September 2011[/editline]
Assuming you put "using namespace std;" in the global namespace, at least.
How can I find out if two names collide?
The latter will clutter the root namespace. Generally use using for the standard library.
[editline]15th September 2011[/editline]
:ninja:
[QUOTE=Richy19;32306656]Doesnt matter, I preffer having the variable name but its just a matter of opinion[/QUOTE]
Actually, there's one [i]really[/i] good reason to put the variables names in there.
[img]http://dl.dropbox.com/u/11093974/Junk/doubledoubledoubledoubledouble.png[/img]
What the fuck are all these doubles!?
[QUOTE=ZeekyHBomb;32304386]You could store the sf::Input reference in the player-class.
Alternatively have an acceptInput-function and do player->acceptInput(App.GetInput).
Any reason the Player resides in heap-memory? You're leaking the memory btw.[/QUOTE]
Hmm? can you help me out? I'm really new to C++ and SFML haha, i have no idea how to store the sf::Input reference in the player-class, also am i leaking memory because i didn't destroy the player class pointer?
[editline]15th September 2011[/editline]
also can you suggest better ways of doing this?
[QUOTE=jalb;32306985]Actually, there's one [i]really[/i] good reason to put the variables names in there.
[img]http://dl.dropbox.com/u/11093974/Junk/doubledoubledoubledoubledouble.png[/img]
What the fuck are all these doubles!?[/QUOTE]Yeah, that suggestion is exactly what made me question the fact that the book said not to specify them. (the book said that they're not needed because documentation should come with your classes in any cases, but reflecting the documentation in your class definitions would also be a fine thing to do)
That and specifying them means you can do..
//! Starts the game's components. /** @param[in] variables A map of variables, used by Boost. **/ gameManager(opt::variables_map const& variables);
With Doxygen. Also,
[QUOTE=Leonmyster;32304376]I just have a general question about the return statement for C++.
Why do we always need to have
[B]return 0;[/B]
Is it bad if I forget to include it at the end of the main?[/QUOTE]
If you forget to include it returns a random return code, which will most likely be ANYTHING but 0.
Returning 0 or 1 allows programs that execute your program (command line, batch files) to check if the program succeeded or failed, which is useful in making sure everything is going okay.
[QUOTE=Bambo.;32307467]am i leaking memory because i didn't destroy the player class pointer?[/QUOTE]
Yes. Use delete <variableName>;
Anybody else happy about the influx of programmers?
[QUOTE=Jookia;32308346]Anybody else happy about the influx of programmers?[/QUOTE]
Why wouldn't you be. Unless they're ignorant or stubborn fuckers then I don't think you should have anything against them, after all they came here to learn (do mind that I know I count as a new C++ programmer, but I'm not really new to C++ and programming in general)
[QUOTE=jalb;32306985]Actually, there's one [i]really[/i] good reason to put the variables names in there.
[img]http://dl.dropbox.com/u/11093974/Junk/doubledoubledoubledoubledouble.png[/img]
What the fuck are all these doubles!?[/QUOTE]
Well yes but in terms of will it work, it will compile. Whether or not its usable is another thing.
[QUOTE=Jookia;32308346]
Anybody else happy about the influx of programmers?[/QUOTE]
The more the better! Besides, someone else might have a different view on a problem, and that's VERY important when programming. Being able to see something from different perspectives.
A lot of knowledge is never bad, and sharing that knowledge with more people never harmed anyone, so long as it is spread without falsifying it of course [:
[QUOTE=TerabyteS_;32308474]Why wouldn't you be. Unless they're ignorant or stubborn fuckers then I don't think you should have anything against them, after all they came here to learn (do mind that I know I count as a new C++ programmer, but I'm not really new to C++ and programming in general)[/QUOTE]
That wasn't sarcasm. I'm genuinely happy seeing new faces around here.
[editline]16th September 2011[/editline]
[QUOTE=T3hGamerDK;32309187]A lot of knowledge is never bad, and sharing that knowledge with more people never harmed anyone, so long as it is spread without falsifying it of course [:[/QUOTE]
I have the same philosophy with software.
[QUOTE=Jookia;32308346]
If you forget to include it returns a random return code, which will most likely be ANYTHING but 0.[/QUOTE]
No. The main function is a special case and will implicitly return 0 if the end of the function is reached without an explicit return statement. This also applies to C99.
[QUOTE=jA_cOp;32309961]No. The main function is a special case and will implicitly return 0 if the end of the function is reached without an explicit return statement. This also applies to C99.[/QUOTE]
Oh? Well I didn't know that.
[CODE] System.out.println("Are you a Male or Female? Just write M or F and press 'Enter' twice");
String pSex;
pSex = in.nextLine();
if (pSex.toUpperCase()=="M"){
System.out.println("Ah, the strong, maybe sexy beast male. - Good.");}
if (pSex.toUpperCase()=="F"){
System.out.println("Ah, the beautiful, kind, wonderful, grateful female. - Good.");}
else{
System.out.println("Ah, the idiot who doesn't know his gender? Get out of here you slug!");
System.exit(0);}
in.nextLine();[/CODE]
I just started learning java (2 days ago), and today I learned how to use user input ( kind of learned), so now I made it so that when user types M or m it shows one message, and when user enters f or F it displays another. and if user presses anything else then it displays other thing and stops everything. Yet I encountered problem. No matter what I enter, M,m,F,f, or anything else it shows the else text and stops program. why? what did I do wrong?
As well as that I also wanted to ask: Why when I use in.nextLine(); - to stop all my further System.out.println texts appear stright away: is there any other maybe better code to use?
Thanks. Oh and btw if I remove whole else with all it's args then no matter what I type at gender question it just continues with other things without showing nor male nor female comment.
[QUOTE=arleitiss;32312404][CODE] System.out.println("Are you a Male or Female? Just write M or F and press 'Enter' twice");
String pSex;
pSex = in.nextLine();
if (pSex.toUpperCase()=="M"){
System.out.println("Ah, the strong, maybe sexy beast male. - Good.");}
if (pSex.toUpperCase()=="F"){
System.out.println("Ah, the beautiful, kind, wonderful, grateful female. - Good.");}
else{
System.out.println("Ah, the idiot who doesn't know his gender? Get out of here you slug!");
System.exit(0);}
in.nextLine();[/CODE]
I just started learning java (2 days ago), and today I learned how to use user input ( kind of learned), so now I made it so that when user types M or m it shows one message, and when user enters f or F it displays another. and if user presses anything else then it displays other thing and stops everything. Yet I encountered problem. No matter what I enter, M,m,F,f, or anything else it shows the else text and stops program. why? what did I do wrong?
As well as that I also wanted to ask: Why when I use in.nextLine(); - to stop all my further System.out.println texts appear stright away: is there any other maybe better code to use?
Thanks. Oh and btw if I remove whole else with all it's args then no matter what I type at gender question it just continues with other things without showing nor male nor female comment.[/QUOTE]Took me a little bit as I just started learning Java last week, but this works.
[code]import java.util.Scanner;
public class Testing
{
public static void main(String[] args)
{
System.out.println("Are you a Male or Female? Just write M or F and press 'Enter' twice");
Scanner input = new Scanner(System.in);
String pSex = input.nextLine();
char firstChar = pSex.charAt(0);
if (firstChar == 'M' || firstChar == 'm') {
System.out.println("Ah, the strong, maybe sexy beast male. - Good.");
}else if (firstChar == 'F' || firstChar == 'f') {
System.out.println("Ah, the beautiful, kind, wonderful, grateful female. - Good.");
}else {
System.out.println("Ah, the idiot who doesn't know his gender? Get out of here you slug!");
System.exit(0);}
}
}[/code]
[QUOTE=Jookia;32310425]Oh? Well I didn't know that.[/QUOTE]
Just wondering what you think personally, should I add return 0; explicitly, or use return EXIT_SUCCESS;, or just let ut return implicitly. Note that I know that all work, it's just that I want your personal opinion on what I should stick with in my conventions.
[QUOTE=thf;32315755]Just wondering what you think personally, should I add return 0; explicitly, or use return EXIT_SUCCESS;, or just let ut return implicitly. Note that I know that all work, it's just that I want your personal opinion on what I should stick with in my conventions.[/QUOTE]
Ambiguity is bad, I say return EXIT_SUCCESS (if possible, it's a C define) simply because you know from reading what it means. C++ doesn't have that define (I don't think), so 0 and 1 are less readable but the best option over having behaviour that's borderline trivia.
[QUOTE=thf;32315755]Just wondering what you think personally, should I add return 0; explicitly, or use return EXIT_SUCCESS;, or just let ut return implicitly. Note that I know that all work, it's just that I want your personal opinion on what I should stick with in my conventions.[/QUOTE]
Use an explicit "return 0;"
A quick google turns up [url=http://stackoverflow.com/questions/1188335/why-default-return-value-of-main-is-0-and-not-exit-success]this stack overflow thread[/url] indicating that EXIT_SUCCESS is exactly equivalent to zero on all platforms and that a return value of zero gives the system a 'success' code (which is not necessarily zero itself). So why choose the option that requires more typing when both are equivalent and equally clear to the reader?
[QUOTE=arleitiss;32312404][CODE] System.out.println("Are you a Male or Female? Just write M or F and press 'Enter' twice");
String pSex;
pSex = in.nextLine();
if (pSex.toUpperCase()=="M"){
System.out.println("Ah, the strong, maybe sexy beast male. - Good.");}
if (pSex.toUpperCase()=="F"){
System.out.println("Ah, the beautiful, kind, wonderful, grateful female. - Good.");}
else{
System.out.println("Ah, the idiot who doesn't know his gender? Get out of here you slug!");
System.exit(0);}
in.nextLine();[/CODE]
I just started learning java (2 days ago), and today I learned how to use user input ( kind of learned), so now I made it so that when user types M or m it shows one message, and when user enters f or F it displays another. and if user presses anything else then it displays other thing and stops everything. Yet I encountered problem. No matter what I enter, M,m,F,f, or anything else it shows the else text and stops program. why? what did I do wrong?
As well as that I also wanted to ask: Why when I use in.nextLine(); - to stop all my further System.out.println texts appear stright away: is there any other maybe better code to use?
Thanks. Oh and btw if I remove whole else with all it's args then no matter what I type at gender question it just continues with other things without showing nor male nor female comment.[/QUOTE]
Strings in Java are not primitive types like int, char, byte, boolean, float, double, long and short.
Therefore, when you're doing this comparison:
[code]pSex.toUpperCase() == "M"[/code]
You're actually comparing two pointers to separate objects when you ought to be comparing their contents.
Luckily, the String class has a built-in method for this:
[code]pSex.toUpperCase().equals("M")[/code]
This should work.
[QUOTE=ROBO_DONUT;32315849]Use an explicit "return 0;"
A quick google turns up [url=http://stackoverflow.com/questions/1188335/why-default-return-value-of-main-is-0-and-not-exit-success]this stack overflow thread[/url] indicating that EXIT_SUCCESS is required to be zero on all platforms and that a return value of zero gives the system a 'success' code (which is not necessarily zero itself). So why choose the option that requires more typing when both are equivalent and equally clear to the reader?[/QUOTE]
What's your reason not to let it just implicitly return? Just curious here :v:
Sorry, you need to Log In to post a reply to this thread.