Skip to main content
gamedev.net
🔒 Locked

Simple C problem - please help

Started by one mind Sep 2, 2006 at 6:24 AM 11 replies 900+ views
Original Post
one mind
one mind
Hi, I am having a strange problem with the function below. For some reason, the if statement isn't working.

void number()
{
	int num = 0, i;
	printf("\nPlease enter a number between 1 and 50: ");
	scanf("%d", #);
	flushall();

	if(num<1 || num>50);
	{
		printf("\nNumber must be between 1 and 50\n\n");
		number();
	}	

	for(i=0; i<num; i++)
	{
		printf("%d, ", i);
	}
}
No matter what number i input, like 5 for example, it outputs "Number must be between 1 and 50". Why is that? The if statements logic seems to be ignored. Any help solving this would be greatly appreciated. Thanks
one mind
one mind
doh!

THANKS :)
one mind
one mind
Ok, now i have a harder problem.

If i enter a number higher than 50, like 51 for example, it correctly says that it must be between 1-50 then it asks for another input. I then input an appropriate number like 5 then it outputs:

0, 1, 2, 3, 4, 5

0, 1, ... ,49, 50, 51

For some reason, it remembers the first incorrect output then loops through that.

I took a look at it in the debugger and just as it is about to leave the function, num changes to 51 or whatever number i input incorreclty. It seems flushall() doesn't work here.

Any help?

Thanks
deffer
deffer
if(num<1 || num>50);
{
printf("\nNumber must be between 1 and 50\n\n");
number();
return;
}

But the right answer is to mount a while() loop aroung the whole function body and use continue instead of the return.
one mind
one mind
This is killing me
one mind
one mind
Thanks deffer, i missed your post :)

Any idea why this happens? Shouldn't flushall remove the 51?

Any further info would be great.
one mind
one mind
Cool, thanks for that :)
Zahlman
Zahlman
0) For Beginners, please.

1) Don't call functions for flow control. Call them to delegate work. The called function will, after all, eventually return.

I really don't understand why "goto thinking" is so prevalent among beginners. It's as though noone is capable of explaining in their tutorials how functions actually work.
one mind
one mind
Quote:
Zahlman... if only everyone was as perfect as you...


Exactly what i was thinking..

Anal

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.