if statement condition ignored in struct member function (SOLVED)

Started by incobalt, Sun 02/11/2008 08:51:33

Previous topic - Next topic

incobalt

It seems my function has decided to stop processing if statement conditions.  I am perplexed.  I can't seem to find any missing brackets or anything that would make if statements go haywire.  I've even tried to get a response out of it by making something purposefully true, but it even seems to be ignoring if(true).  Could anybody help me out here and point out my, possibly idiot mistake in the following function (my finageling, you can see in the last set of if statements.)

Code: ags

String GameTime::GetDuration(int sec)
{
	if(sec < 60 && sec > 1)
		return String.Format("%d seconds", sec);
	if(sec == 1)
		return "1 second";
	if(sec < 1)
		return "no time at all";
	mDateTime holder;
	holder.Second = 0;
	holder.Minute = 0;
	holder.Hour = 0;
	holder.DayOfMonth = 0;
	holder.Month = 0;
	holder.Year = 0;
	holder.AddSec(sec);
	String rStr;
	rStr = "";
	if(holder.Year != 0){
		rStr.Append(String.Format("%d year",holder.Year));}
	if(holder.Year > 1){
		rStr.Append("s");}
	if(holder.Month != 0)
	{
		if(rStr != ""){
			rStr.Append(", ");}
		rStr.Append(String.Format("%d month",holder.Month));
	}
	if(holder.Month > 1){
		rStr.Append("s");}
	if(holder.DayOfMonth != 0)
	{
		if(rStr != ""){
			rStr.Append(", ");}
		rStr.Append(String.Format("%d day",holder.DayOfMonth));
	}
	if(holder.DayOfMonth > 1){
		rStr.Append("s");}
	if(holder.Hour != 0)
	{
		if(rStr != ""){
			rStr.Append(", ");}
		rStr.Append(String.Format("%d hour",holder.Hour));
	}
	if(holder.Hour > 1){
		rStr.Append("s");}
	if(holder.Minute != 0)
	{
		if(rStr != ""){
			rStr.Append(", ");}
		rStr.Append(String.Format("%d minute",holder.Minute));
	}
	if(holder.Minute > 1){
		rStr.Append("s");}
	Display("%d", holder.Second != 0); //this executes
	if(true)
	{
		Display("%s", rStr); //this does not execute
		if(rStr != ""){
			rStr.Append(", ");}
		rStr.Append(String.Format("%d second",holder.Second));
	}
	if(holder.Second > 1)
		rStr.Append("s");
	
	return rStr;
}


I apologize for the long block of code.  I'm sure the error is in there somewhere, but I can't find it.  The if(true) statement was previously if(holder.Second != 0), which evaluates to true as well, according to the Display() call.

Gilbert

The reason is, someString.Append("blah") returns something that you need to hold with a String variable, if you just write someString.Append("blah"); it does nothing.

Instead, in your case, ALL the rStr.Append() line should be something like:

rStr=rStr.Append(blah bla bla);

DoorKnobHandle

I think the problem is that this block of code:

Code: ags

if(sec < 60 && sec > 1)
	return String.Format("%d seconds", sec);
if(sec == 1)
	return "1 second";
if(sec < 1)
	return "no time at all";


Will in every case EXCEPT that the parameter sec is => 60 terminate the function. The return keyword not only returns a variable but also stops the rest of the function from running, TygerWulf!

Khris

I'm pretty sure sec is bigger than 60 in most cases, why else all the year/month/day stuff.
And calling Display(""); won't in fact do anything, I've just tried.

incobalt

Aha!  See, I knew it was something stupid like that.  I'll make sure I test with Display including some extra text at the beginning from now on  ;)  Yeah, the point of the first three if statements is to catch if the number is already less than 60 and therefore doesn't need to go through the whole bif if statement jungle  ::)  Anyway, thanks everyone, my code is running ok now :)

SMF spam blocked by CleanTalk