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
🔒 Locked
Simple C problem - please help
Advertisement
Original Post
Hi, I am having a strange problem with the function below. For some reason, the if statement isn't working.
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
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
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement