Saturday, May 22, 2010

For some reason, my compiler is giving me trouble when I put a scanf() statement within any kind of loop.?

I use Dev-Cpp Bloodshed C/C++ compiler. When I try to use the scanf() function in an IF loop, it just seems to bypass the scanf() statement completely. If I use it outside of an IF it works just fine though. Here is the code...








if(ans==1){


printf("you chose number one.");


printf("Please enter your guess: ");


scanf("%c", %26amp;guesslet);


printf("%c", guesslet);


}





...that scanf() wouldn't work. Does anyone know why this is? Thanks a lot.

For some reason, my compiler is giving me trouble when I put a scanf() statement within any kind of loop.?
first, please check if you already include necessary library into your code. %26lt;stdio.h%26gt; are almost compulsary for any C programming but could check if you have %26lt;math.h%26gt; and %26lt;stdlib.h%26gt;.





as per my understanding from this code, i can fore see that you try to read and write input given by user without any other operation involved thus i suggest you to make your code like this:





--------------------------------------...


if(ans==1)


{


printf("you chose number one.");


printf("Please enter your guess: ");


}





elseif(ans==2)


{ .......


}





scanf("%c", %26amp;guesslet);


printf("%c", guesslet);


--------------------------------------...





i think the code work much better this way. hope it's work. :)
Reply:It cannot happen. Post the full code, the problem could be somewhere else
Reply:check to see if you have


#include %26lt;stdio.h%26gt;


in your code

flowers on line

No comments:

Post a Comment