简体   繁体   中英

Capture enter as an input (for int and char variables)

Let's suppose I have a structure called books , with both string and int variables .

I want to do the following:

  1. Print the actual value stored in the variable.
  2. Give the option to insert a new value.
  3. If the user presses 'Enter', the value remains the same.

I tried to do it with scanf , but it is not able to receive an empty input. If it was, I could simply do:

printf("Current value: %d     New value: ",books.intVar);
scanf("%d",aux);
if (aux) {
     books.intVar = aux;
}

And something similar with the strings, but using strcpy() function to assign the new value.

I am pretty sure the solution to this problem is a combination of gets() and sscanf() but I don't see how should I use them to obtain the result I am looking for.

Appreciate any help.

You can use fgets() instead, like this

char line[100];
int  value;
if (fgets(line, sizeof(line), stdin) != NULL)
 {
    if (line[0] == '\n')
        handleEmptyLine();
    else
     {
        value = strtol(line, NULL, 10);
        fprintf(stdout, "New Value: %d\n", value);
        /* Do whatever you want with value */
     }
  }

While the same code would probably work with gets() it's a really bad and unnecessary thing to do, because you risk buffer overflow with gets() which has no way to limit input length, while fgets() does allow you to set a maximum value for the length of the destination buffer.

You should note that fgets() does read the '\\n' character at the end of the input, which you can use conveniently in your case to check whether the line is empty, although it's not enough because an empty line could also be a bunch of white space characters, so the line[0] == '\\n' test will only work if the user only presses the Enter / Return key, hence it would be safer to do something like

char  buffer[100];
char *line;
int   value;

if ((line = fgets(buffer, sizeof(buffer), stdin)) != NULL)
 {
    while ((line[0] != '\0') && (isspace((int) line[0]) != 0))
        line++;
    if (line[0] == '\0')
        handleEmptyLine();        
    else
     {
        value = strtol(line, NULL, 10);
        fprintf(stdout, "New Value: %d\n", value);
        /* Do whatever you want with value */
     }
  }

Use fgets() instead of scanf() . And if you want to capture empty input, you can compare it to '\\n' .

I just found a much shorter and easier way to do the same thing @iharob suggests:

char aux[100];

// Sample data:
char name[100];
int number = 250;
strcpy(name,"Sample name");

// Interger case:
gets(aux);
sscanf(aux,"%d",number);

// String case:
gets(aux);
sscanf(aux,"%s",name);

When the program asks for an int or a string and we don'n provide it (pressing Enter instead), the value of the variables number and name won't change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM