0
When I put a char on scanf
the program printa lot of "height" and does not give me the chance to choose another value, only enters an infinite loop.
Code:
int main()
{
int result;
while(result < 1 || result > 8)
{
printf("height: ");
scanf("%d", &result);
}
if(result >= 1 && result <= 8)
{
ft_mario(result);
}
}
I’ve tried to do with char
instead of int and use the result - '0' but did not give either, it does not printa dnv when wrong only for the program, and there is another way that how many characters I put it will give x print quantities of height.
I know you can take the if
there and play only the function but also does not change anything.
How do I solve this problem ?
I’m not sure I understand, but if you use several
scanf("%c", &variavel);
then you’ll have trouble with garbage in the buffer.– Júlio Evêncio
wanted to make a loop until the user put the right answer
– luisp-29
In case a loop read
char
, right? I’ll make an example here.– Júlio Evêncio
Here an example using
char
. I noticed I usedgetchar()
to take out the\n
ofbuffer
.– Júlio Evêncio
i understood what you did but in case I need q o o Usario type a number and if in case he type a letter I show the scanf dnv that q ta complicating and still have the special characters ai complica more I thought of using the table ascii but tbm n had success
– luisp-29
Now I understand the problem, you need to clean up the
buffer
. Placesetbuf(stdin, NULL);
or a custom function after thescanf
to clean the buffer.– Júlio Evêncio
guy did it there and it worked agr the problem is when I put more than one number he takes the first number and ignores what comes dps and he was not to run with big number only from 1 to 8
– luisp-29
Can you send the code as it is now? You can ask a question with it being a little clearer.
– Júlio Evêncio
soon the delay, we were able to make work using the recreation of setbuf and tbm with fflush amazenando the input in a variable( input[50]) and passing it to an act,i ae only played in the function she and was successful and with the recreation of the function was good tbm, in the end until the setbuf q was giving problem worked with atoi, vlw mann this buffer stop helped a lot!!!
– luisp-29