简体   繁体   中英

Char array taking input putting remaining data to consecutive char array

I am having a structure defined in person.h file

struct person{
    char firstName[11];
    char familyName[21];
    char telephone[11];
    int isStudent;
};

Now I want to take input of firstName,familyName etc.

So I did something like this :

void AddRecord(struct person person[20],int currentElemetCount,int maxCount){
    getFirstName(person[currentElemetCount].firstName);
    getFamilyName(person[currentElemetCount].familyName);
}

In other commons.c file I define these functions,

 void getFirstName(char firstName[10]){
    printf("Enter First Name : ");
    scanf("%10s", firstName);
 }

 void getFamilyName(char familyName[20]){
    printf("Enter Family Name : ");
    scanf("%20s", familyName);
 }

But when I execute this program, in actual case I want that if user enter more than 10 characters for first name then simply ignore them. But here they are getting assigned to familyName and am not able to input anything for familyName.

What can be the reason ? Please help.

Say if input is "avshgvdshvdhsdhsdhsdhdh" a very long string. Then I want firstName char array as "avshgvdshv" . But with this code remaining "dhsdhsdhsdhdh" string is getting assigned to familyName array, which I dont want.

  1. You need to clear stdin (the input stream)
  2. Use fgets

The excess data (over 10 characters) remains in the input stream and are used the next time you call scanf .

To clear stdin

int ch;
while ((ch = getchar()) != '\n' && ch != EOF);

A portion of code I used in my own programs to get input using fgets and clearing the input stream to allow for other input:

fgets(c, size, stdin);
if (c[strlen(c) - 1] != '\n')
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
}

The fgets function takes input until it runs into a newline ( \\n ) or the maximum number of bytes readable have been read (in your case 10). Usually fgets gives a newline terminated string ( char* with \\n at the end). This code snippet checks the input to see if the last character is a newline (indicating the length was OK) or another character (indicating it was too long and there is extra data in the stream).

You can have scanf discard all chars after the first 10 this way

scanf("%10s%*s", firstName);

The %*s in this case means scan a string but don't store it to a variable. Mind you that in this case scanf will stop scanning on any whitespace char. If the strings you enter have spaces that will cause a problem

You need to clear the contents of the standard input stream. This can be done changing these:

scanf("%10s", firstName);
scanf("%20s", familyName);

to

scanf("%10s%*[^\n]", firstName);scanf("%*c");
scanf("%20s%*[^\n]", familyName);scanf("%*c");

The %*[^\\n] intructs scanf to scan and discard everything until a newline character ( '\\n' ). %*c instructs scanf to scan and discard a character which, in this case, is probably the newline character.

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