c - How do I return a printf statement if the user inputs nothing? -


i want execute statement based on input of user:

#include <stdio.h> #include <string.h>  void main() {     char string_input[40];    int i;     printf("enter data ==>  ");       scanf("%s", string_input);     if (string_input[0] == '\n') {       printf("error - no data\n");        }    else if (strlen(string_input) > 40) {       printf("hex equivalent       ");       }    else {       printf("hex equivalent       ");       }  } 

when run it, , press enter, goes new line instead of saying "error - no data".

what do?

cannot use fgets have not gone on in class.

use

char enter[1]; int chk = scanf("%39[^\n]%c", string_input, enter); 

but string_input not have '\n' inside. test

   if (string_input[0] == '\n') {        printf("error - no data\n");    } 

will have changed to, example

   if (chk != 2) {        printf("error - bad data\n");    } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -