here there is no cases where there is no return but the compiler doesn't know it
however this is a warning and it doesn't affect compilation (the program will work normally except if it get to a place where it doesn't return -it causes a runtime error-)
you can correct the second case by using else in place of if(x<5) or default: if you are in a switch
On line 5, x has to be less than five, so there is no sense in testing, and the added benefit is that the compiler won't complain, since the function will always return a value (even if it is the wrong value, it returns one).
thanks both of you... I got it ... why warning was coming.,...
But m still confuse that how i can fix it using if....
Here is the code please correct me...
int life ()
{
int l;
if(flag==0&&top==0)
{
cout<<"\n AAPKE DO LIFE LINE HAIN:";
cout<<" (1) COMPUTER ASK";
cout<<" (2) FIFTY-FIFTY";
cout<<"\n PRESS (1) OR (2) : ";
cin>>l;
return (l);
}
if(flag==0&&top!=0)
{
cout<<"\n APPKE KA PASS 1 LIFE LINE HA";
cout<<"\n FIFTY-FIFTY \n Press 2 to use it";
cin>>l;
return (l);
}
if(flag=!0&&top==0)
{
cout<<"\n APPKE KA PASS 1 LIFE LINE HA";
cout<<"\n COMPUTER ASK \n Press 1 to use it";
cin>>l;
return (l);
}
if(flag!=0&&top!=0)
{
cout<<"\n APPKE KA PASS KOI LIFE LINE NAHI";
l=0;
return (l);
}
}
if(flag=!0&&top==0) <- that probably should be !=, not =!.
I would get rid of the && nonsense (I hate using compound conditionals unless I absolutely have to) and move the return outside of the conditionals so you don't get the warning (you're always returning l, so no need to put that in any if blocks.
int life ()
{
int l;
if(flag==0)
{
if(top == 0)
{
cout<<"\n AAPKE DO LIFE LINE HAIN:";
cout<<" (1) COMPUTER ASK";
cout<<" (2) FIFTY-FIFTY";
cout<<"\n PRESS (1) OR (2) : ";
cin>>l;
}
else
{
cout<<"\n APPKE KA PASS 1 LIFE LINE HA";
cout<<"\n FIFTY-FIFTY \n Press 2 to use it";
cin>>l;
}
}
else // flag != 0
{
if(top==0)
{
cout<<"\n APPKE KA PASS 1 LIFE LINE HA";
cout<<"\n COMPUTER ASK \n Press 1 to use it";
cin>>l;
}
else
{
cout<<"\n APPKE KA PASS KOI LIFE LINE NAHI";
l=0;
}
}
return l;
}
If this is for a game, I'd recommend a lib like SFML ( http://sfml-dev.org/ ). This will help with background music, sound effects, and even graphics/input if you want it.
If you just want audio and not any of the other stuff, another option is BASS ( http://www.un4seen.com/ ). It's audio capabilities are greater than that of SFML's, but doesn't have the extra graphics stuff (it's audio-only).
Both are pretty easy to use once you get them up and running.
i try whenever I can to only have ONE return statement in a method.
That's my preference, too. Having a single exit point for a function makes it easier to be sure that you've done any cleanup that you need to do, and that you've left everything in the correct state before exitting the function.
I admit that it's a slightly old-fashioned habit, from my days as a C developer. With well-designed OO code, making good use of destructors, RAII, etc, it should be less of an issue. Although I don't think it's a bad habit to stick to, in any case - especially since I still find myself having to work with legacy code that hasn't been as well-written as it should have been, from time to time.
I try to do that as well, except when it's less practical than the alternative.
For example... I'll often have "early outs" that exit the function before any of the actual work is done... rather than have the bulk of the routine nested in a few layers of ifs:
1 2 3 4 5 6 7
void func()
{
if(!readyToDoThisCode) return;
if(doingThisNowHasNoEffect) return;
//... do actual code
}
Also if it comes down to multiple returns or compound if statements, I'll generally favor multiple returns.
Then try to build a basic example program from the tutorial on their site (to make sure it's working correctly).
From there, the on-site guides are as good as anything I could tell you. Just take a look at the sf::Music class and look at the examples of how to use it.