質問

Im trying to use gets() to get a string from the user but the program seems to be passing right over gets(). There is no pause for the user to give input. Why is gets() not doing anything?

char name[13];
printf("Profile name: ");
gets(name);
printf("\n%s", name);
役に立ちましたか?

解決 4

You get lot of troubles using gets()

Instead go for fgets()

fgets(name,13,stdin);  

See this SO question Why is the gets function so dangerous that it should not be used?

The reason why fgets() does not work, may be you are not handling the newline left behind by scanf in your previous statements.

You can modify your scanf format string to take it into account: scanf("%d *[^\n]", &N);

*[^\n] says to ignore everything after your integer input that isn't a newline, but don't do anything with the newline (skip it).

When you use scanf("%d",&num) you hit 13 and enter and 13 is stored in num and the newline character is still in the input buffer when you read fgets from stdin it treats \n as the data you have entered and the fgets() statement is skipped

You cannot flush input buffer however you can do this fseek(stdin,0,SEEK_END); add this before your every fgets statement

他のヒント

Call getchar() before you call gets() or fgets(). Since gets() or fgets() is getting skipped due to an already present '\n' from previous inputs in stdin, calling getchar() would lead to itself getting skipped instead of gets() or fgets() or any other similar function. But remember its more of a hack and not a standard solution (I think so), and also use of gets() is forbidden.

        printf("\nEnter a String: ");
        getchar();
        //fgets(inputString, 100, stdin);
        gets(inputString);
        printf("\n%s", inputString);

It's because gets() it's so incredibly dangerous to use, that some C libraries have removed it completely and replaced it with a version that does nothing.

Use fgets() instead.

Use fflush(stdin); before gets()

Take a look at gets() reference

Get string from stdin

Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

The newline character, if found, is not copied into str.

A terminating null character is automatically appended after the characters copied to str.

Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).

So, basically gets() is not only unsafe (can lead to buffer overflows) but also reads what's in the input buffer.

I recommend you to use fgets(), but if you want a quick (and lazy and stupid) solution, just flush the input buffer:

char name[13];
printf("Profile name: ");
fflush(stdin);
gets(name);
printf("\n%s", name);

Add this below function to your code and enjoy it.

string GetString()
{
   char ch;
   string Line="";

   while(1)
   {
       ch=getchar();

       if(ch=='\n')
          break;
       else
          Line+=ch;
    }

    return Line;
}

This function can effect all Spaces and Backspaces too!!!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top