problem with gets()

2019-08-26 20:54发布

I am trying to take input data and print it using structures. when i run this code it is not taking input for lastname. It directly asks to enter input for firstname. Can someone help me with this. Thank you.

address.c

typedef struct 
{
  char lname[25];
  char fname[20];
  char address[20];
  char phonenumber[20];
}addressbook;

addressbook a;
int main()
{
 printf("enter details:\n");
   printf("enter lastname:\n");
   gets(a.lastname);
   printf("enter firstname:\n");
   gets(a.firstname);
   printf("enter address:\n");
   gets(a.address);
   printf("enter phone number:\n");
   gets(a.phonenumber);
   printf("lastname:%s\n",a.lastname);
   printf("firstname: %s\n", a.firstname);
   printf("address:%s\n", a.address);
   printf("phone number:%s\n", a.phonenumber);
}

When I run this it is not waiting to enter the lastname. it directly goes to enter firstname.

标签: c gets
1条回答
Juvenile、少年°
2楼-- · 2019-08-26 21:34

That code won't even compile as it stands, producing (in my environment):

pax@pax-desktop:~$ gcc -Wall -Wextra -o qq qq.c
qq.c: In function ‘main’:
qq.c:12:2: warning: implicit declaration of function ‘printf’
qq.c:12:2: warning: incompatible implicit declaration of built-in function ‘printf’
qq.c:14:4: warning: implicit declaration of function ‘gets’
qq.c:14:10: error: ‘addressbook’ has no member named ‘lastname’
qq.c:16:10: error: ‘addressbook’ has no member named ‘firstname’
qq.c:21:28: error: ‘addressbook’ has no member named ‘lastname’
qq.c:22:31: error: ‘addressbook’ has no member named ‘firstname’
qq.c:25:1: warning: control reaches end of non-void function

You should always (at least initially) compile with a high warning level and take note of what the compiler is telling you:

  • you should include stdio.h if you're going to use printf and gets.
  • you should use consistent field names in your structure.
  • you should return something from non-void functions (technically this is not necessary under later versions of the standard but it's still a good idea for portability).

In addition, you may want to consider the following:

  • int main() is not one of the two canonical forms of the main function. While the standard allows for implementation-defined extra ones, the "correct" one for this particular case would be int main (void).
  • gets is a dangerous function since there is no way to prevent buffer overflow, making your code insecure. For example, if I entered a thousand characters for my first name, it may well screw up your program by overwriting huge chunks of accounting information on the stack (like return addresses for example). You can find a safer input function here.

Making most of those changes, you would end up with something like:

#include <stdio.h>

typedef struct {
    char lastname[25];
    char firstname[20];
    char address[20];
    char phonenumber[20];
}addressbook;

addressbook a;
int main (void) {
    printf("enter details:\n");

    printf("enter lastname:\n");
    gets(a.lastname);

    printf("enter firstname:\n");
    gets(a.firstname);

    printf("enter address:\n");
    gets(a.address);

    printf("enter phone number:\n");
    gets(a.phonenumber);

    printf("lastname:%s\n",a.lastname);
    printf("firstname: %s\n", a.firstname);
    printf("address:%s\n", a.address);
    printf("phone number:%s\n", a.phonenumber);

    return 0;
}

which compiles and runs okay, although it still has the serious buffer overflow vulnerability. If you want to fix that, you can refer to the link I gave.

查看更多
登录 后发表回答