Hi friends, well... i'm asking for another help from you guys.hope you will...

My friend sent me a email asking why the following code doesn't work properly.I'm also couldn't understand what's happening..When this program run it supposed to prompt two times to enter values.but its actually prompt once. I tried to clean input buffer,but it seems like the same.please anyone can help.I'm sure you guys can..thank you in advanced. i'm running this on ubuntu.

#include<stdio.h>
#include<string.h>
void main()
{
    
    char check[5];                       
                                                 
    prter:intf("En-        ");    
   scanf("%s",check);            
    printf("%s\n",check);
    printf("%.2s\n",check);
    printf("Re-Enter:-    ");
    fflush(stdin);
   gets(check);
    printf("%s\n",check);
}

scanf("%s" ...) stops reading keyboard input at the first space. So if you type "John Doe" all scanf() will return is "John". The word "Doe" will be left in the keyboard buffer, so the next time scanf() or gets() is called it will grab "Doe" and not let you type anything.

If you want the user to enter two or more words, like "John Doe", then use fgets() instead of either scanf() or gets(). Note that gets() is never recommended because it can cause buffer overflow (entering more characters than the buffer can hold.)

you can use

scanf("%[^\n]",...);

as well to scan a whole line.

commented: good point +36

thanx for all responing ... i was unable to connect internet last days.. thank you you again..

You're welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.