Search code examples
cstringuser-input

How to check if user pressed Enter key ?


#include <stdio.h>
#include <string.h>
#include "formatCheck.h"
int main()
    {
    char input[32];
    char format[32]
    printf("enter your format : ");
    fgets(input,sizeof(input),stdin);
    sscanf(input,"%s",format);
        //my problem
        //if user don't enter format it will exit.
         if()
            {
            return 0;
            }
    }

How can I check if user doesn't input anything (just Enter key). Sorry about English. Thanks.


Solution

  • When user hits only enter, input[0] contains \n

    fgets(input,sizeof(input),stdin);
      if(input[0]=='\n') printf("empty string");