1 Warning

c:\users\q\downloads\kbc project.cpp(134) : warning C4715: 'life' : not all control paths return a value


In your function life there are cases where there is no return
for example
1
2
3
4
5
int life(int x)
{
     if(x==5)    return 5;
     if(x>5) return 6;
}

here if x<5 there is no return
another example:
1
2
3
4
5
6
int life(int x)
{
     if(x==5)    return 5;
     if(x>5) return 6;
     if(x<5) return 4;
}

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
JewelCpp + 1

Typically you can best avoid it by making sure there is an unconditional return at the end of the function:

1
2
3
4
5
6
int life(int x)
{
    if(x==5) return 5;
    if(x>5)  return 6;
    return 4;
}

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).

Hope this helps.
Last edited on
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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
	}

}	
What happens if none of those if conditions are true?
You have a mistake on line 20:

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.

Here is the code, changed up a bit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
}
Disch
Thanks Alot.....
you are my new teacher...
Disch Will you tell me how i can add .wav mp3 or any kind of music in C++
I know only one music which is \a :P
Last edited on
The best way is to get an audio lib.

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.
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.
Last edited on
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.

IE, rectangle collision:

1
2
3
4
5
6
7
8
9
bool rectCollide(const Rect& a, const Rect& b)
{
    if(a.l > b.r) return false;
    if(a.r < b.l) return false;
    if(a.t > b.b) return false;
    if(a.b < b.t) return false;

    return true;
}



So yeah it's a good general rule... but it's definitely not an "always do it this way" rule. At least not IMO.
Last edited on
Ditch I am new to sfml will you tell me more about this?
and i am using MS visual C++
You'll need to download and install SFML.

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.
thanks disch
Im going to check this.
Topic archived. No new replies allowed.