struct的字符串输入扫描

uajslkp6  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(124)

This is the question consisting of 1st stage, 2nd stage and 3rd stage.
这是第一阶段的代码,运行良好。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

struct PERSON {
char name[20];
char phoneNumber[15];
char birthDate[8];
};

struct PERSON record[100];
int personCount = 0;

char nameValidation(char name[]) {
rewind(stdin);
scanf("%s", name);

for (int i = 0; i < strlen(name); i++) {
    if (strlen(name) > 20) {
        printf("Name must be less than 20 characters. Please try again\n");
        printf("Name: ");
        rewind(stdin);
        return nameValidation(name);
    }
    if ((name[i] < 'a' || name[i] > 'z') && (name[i] < 'A' || name[i] > 'Z')) {
        printf("Invalid name. Please try again.\n");
        printf("Name: ");
        rewind(stdin);
        return nameValidation(name);
    }
}
return *name;
}
char phoneNumValidation(char phoneNumber[]) {
rewind(stdin);
scanf("%s", phoneNumber);

for (int i = 0; i < strlen(phoneNumber); i++) {
    if (strlen(phoneNumber) > 15) {
        printf("Phone number must be less than 15 characters. Please try again\n");
        printf("Phone number: ");
        rewind(stdin);
        return phoneNumValidation(phoneNumber);
    }
    if (phoneNumber[i] < '0' || phoneNumber[i] > '9') {
        printf("Invalid phone number. Please try again.\n");
        printf("Phone number: ");
        rewind(stdin);
        return phoneNumValidation(phoneNumber);
    }
}
return *phoneNumber;
}
char birthDateValidation(char birthDate[]) {
rewind(stdin);
scanf("%s", birthDate);

for (int i = 0; i < strlen(birthDate); i++) {
    if (strlen(birthDate) > 8) {
        printf("Birth date must be less than 8 characters. Please try again\n");
        printf("Birth date: ");
        rewind(stdin);
        return birthDateValidation(birthDate);
    }
    if (birthDate[i] < '0' || birthDate[i] > '9') {
        printf("Invalid birth date. Please try again.\n");
        printf("Birth date: ");
        rewind(stdin);
        return birthDateValidation(birthDate);
    }
}
return *birthDate;
}
int choiceValidation(char statement[], int min, int max) {
    int choice;
    printf("%s", statement);
    rewind(stdin);
    scanf("%d", &choice);
    if (choice < min || choice > max) {
        printf("Invalid choice. Please try again.\n");
        return choiceValidation(statement, min, max);
    }
    return choice;
}

void registration() {
printf("Name: ");
nameValidation(record[personCount].name);

printf("Phone_number: ");
phoneNumValidation(record[personCount].phoneNumber);

printf("Birth: ");
birthDateValidation(record[personCount].birthDate);

personCount++;
printf("<<%d>>\n", personCount);
}
void showAll() {
for (int i = 0; i < personCount; i++) {
    printf("%s %s %s\n", record[i].name, record[i].phoneNumber, record[i].birthDate);
}
}
void deleteFunction() {
char name[20];
printf("Name: ");
nameValidation(name);
for (int i = 0; i < personCount; i++) {
    if (strcmp(name, record[i].name) == 0) {
        for (int j = i; j < personCount; j++) {
            strcpy(record[j].name, record[j + 1].name);
            strcpy(record[j].phoneNumber, record[j + 1].phoneNumber);
            strcpy(record[j].birthDate, record[j + 1].birthDate);
        }
        personCount--;
        return;
    }
}
}
void findByBirth() {
char birthMonth[3], getBirthMonth[3];
int position = 5, length = 2, check = 0;

printf("Birth month: ");
birthDateValidation(birthMonth);
for (int i = 0; i < personCount; i++) {
    while (check < length) {
        getBirthMonth[check] = record[i].birthDate[position + check - 1];
        check++;
    }
    if (strncmp(birthMonth, getBirthMonth, 2) == 0) {
        printf("%s %s %s\n", record[i].name, record[i].phoneNumber, record[i].birthDate);
    }
}

}

int main() {
int menuNum;
do {
    printf("*****Menu*****\n");
    printf("<1.Registration><2.ShowAll><3.Delete><4.FindByBirth><5.Exit>\n");

    menuNum = choiceValidation("Enter the menu number: ", 1, 5);

    switch (menuNum) {
    case 1:registration(); break;
    case 2:showAll(); break;
    case 3:deleteFunction(); break;
    case 4:findByBirth(); break;
    case 5:exit(-1); break;
    }
} while (menuNum != 5);

return 0;
}

这是第二阶段的代码,我根据问题进行了修改,但遇到了错误。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

struct PERSON {
char *name;
char *phoneNumber;
char *birthDate;
};

struct PERSON **record;
int personCount = 0;

char nameValidation(char name[]) {
rewind(stdin);
scanf("%s", name);

for (int i = 0; i < strlen(name); i++) {
    if (strlen(name) > 20) {
        printf("Name must be less than 20 characters. Please try again\n");
        printf("Name: ");
        rewind(stdin);
        return nameValidation(name);
    }
    if ((name[i] < 'a' || name[i] > 'z') && (name[i] < 'A' || name[i] > 'Z')) {
        printf("Invalid name. Please try again.\n");
        printf("Name: ");
        rewind(stdin);
        return nameValidation(name);
    }
}
return *name;
}
char phoneNumValidation(char phoneNumber[]) {
rewind(stdin);
scanf("%s", phoneNumber);

for (int i = 0; i < strlen(phoneNumber); i++) {
    if (strlen(phoneNumber) > 15) {
        printf("Phone number must be less than 15 characters. Please try again\n");
        printf("Phone number: ");
        rewind(stdin);
        return phoneNumValidation(phoneNumber);
    }
    if (phoneNumber[i] < '0' || phoneNumber[i] > '9') {
        printf("Invalid phone number. Please try again.\n");
        printf("Phone number: ");
        rewind(stdin);
        return phoneNumValidation(phoneNumber);
    }
}
return *phoneNumber;
}
char birthDateValidation(char birthDate[]) {
rewind(stdin);
scanf("%s", birthDate);

for (int i = 0; i < strlen(birthDate); i++) {
    if (strlen(birthDate) > 8) {
        printf("Birth date must be less than 8 characters. Please try again\n");
        printf("Birth date: ");
        rewind(stdin);
        return birthDateValidation(birthDate);
    }
    if (birthDate[i] < '0' || birthDate[i] > '9') {
        printf("Invalid birth date. Please try again.\n");
        printf("Birth date: ");
        rewind(stdin);
        return birthDateValidation(birthDate);
    }
}
return *birthDate;
}
int choiceValidation(char statement[], int min, int max) {
    int choice;
    printf("%s", statement);
    rewind(stdin);
    scanf("%d", &choice);
    if (choice < min || choice > max) {
        printf("Invalid choice. Please try again.\n");
        return choiceValidation(statement, min, max);
    }
    return choice;
}

void registration() {
printf("Name: ");
nameValidation(record[personCount].name);

printf("Phone_number: ");
phoneNumValidation(record[personCount].phoneNumber);

printf("Birth: ");
birthDateValidation(record[personCount].birthDate);

personCount++;
printf("<<%d>>\n", personCount);
}
void showAll() {
for (int i = 0; i < personCount; i++) {
    printf("%s %s %s\n", record[i].name, record[i].phoneNumber, record[i].birthDate);
}
}
void deleteFunction() {
char name[20];
printf("Name: ");
nameValidation(name);
for (int i = 0; i < personCount; i++) {
    if (strcmp(name, record[i].name) == 0) {
        for (int j = i; j < personCount; j++) {
            strcpy(record[j].name, record[j + 1].name);
            strcpy(record[j].phoneNumber, record[j + 1].phoneNumber);
            strcpy(record[j].birthDate, record[j + 1].birthDate);
        }
        personCount--;
        return;
    }
}
}
void findByBirth() {
char birthMonth[3], getBirthMonth[3];
int position = 5, length = 2, check = 0;

printf("Birth month: ");
birthDateValidation(birthMonth);
for (int i = 0; i < personCount; i++) {
    while (check < length) {
        getBirthMonth[check] = record[i].birthDate[position + check - 1];
        check++;
    }
    if (strncmp(birthMonth, getBirthMonth, 2) == 0) {
        printf("%s %s %s\n", record[i].name, record[i].phoneNumber, record[i].birthDate);
    }
}

}

int main() {
int menuNum;

printf("Max_num: ");
scanf("%d", &max_num);

do {
    printf("*****Menu*****\n");
    printf("<1.Registration><2.ShowAll><3.Delete><4.FindByBirth><5.Exit>\n");

    menuNum = choiceValidation("Enter the menu number: ", 1, 5);

    switch (menuNum) {
    case 1:
        if(personCount < n)
        {
            registration(n); 
            personCount++;
            break;
        }
        else
        {
            printf("OVERFLOW\n");
            break;
        }
    case 2:showAll(); break;
    case 3:deleteFunction(); break;
    case 4:findByBirth(); break;
    case 5:exit(-1); break;
    }
} while (menuNum != 5);

return 0;
}

我在编译时收到了错误消息,所以我按照错误消息修改了所有的. to -〉。
而且它可以被编译和运行。
我设法输入max_num和menumum。
当我输入menumum 1(void registration)时,我试图输入名称,但程序立即停止。
我哪里做错了?
这是我将所有.更改为-〉后的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

struct PERSON {
char *name;
char *phoneNumber;
char *birthDate;
};

struct PERSON **record;
int personCount = 0;

char nameValidation(char name[]) {
rewind(stdin);
scanf("%s", name);

for (int i = 0; i < strlen(name); i++) {
    if (strlen(name) > 20) {
        printf("Name must be less than 20 characters. Please try again\n");
        printf("Name: ");
        rewind(stdin);
        return nameValidation(name);
    }
    if ((name[i] < 'a' || name[i] > 'z') && (name[i] < 'A' || name[i] > 'Z')) {
        printf("Invalid name. Please try again.\n");
        printf("Name: ");
        rewind(stdin);
        return nameValidation(name);
    }
}
return *name;
}
char phoneNumValidation(char phoneNumber[]) {
rewind(stdin);
scanf("%s", phoneNumber);

for (int i = 0; i < strlen(phoneNumber); i++) {
    if (strlen(phoneNumber) > 15) {
        printf("Phone number must be less than 15 characters. Please try again\n");
        printf("Phone number: ");
        rewind(stdin);
        return phoneNumValidation(phoneNumber);
    }
    if (phoneNumber[i] < '0' || phoneNumber[i] > '9') {
        printf("Invalid phone number. Please try again.\n");
        printf("Phone number: ");
        rewind(stdin);
        return phoneNumValidation(phoneNumber);
    }
}
return *phoneNumber;
}
char birthDateValidation(char birthDate[]) {
rewind(stdin);
scanf("%s", birthDate);

for (int i = 0; i < strlen(birthDate); i++) {
    if (strlen(birthDate) > 8) {
        printf("Birth date must be less than 8 characters. Please try again\n");
        printf("Birth date: ");
        rewind(stdin);
        return birthDateValidation(birthDate);
    }
    if (birthDate[i] < '0' || birthDate[i] > '9') {
        printf("Invalid birth date. Please try again.\n");
        printf("Birth date: ");
        rewind(stdin);
        return birthDateValidation(birthDate);
    }
}
return *birthDate;
}
int choiceValidation(char statement[], int min, int max) {
    int choice;
    printf("%s", statement);
    rewind(stdin);
    scanf("%d", &choice);
    if (choice < min || choice > max) {
        printf("Invalid choice. Please try again.\n");
        return choiceValidation(statement, min, max);
    }
    return choice;
 }

void registration() {
printf("Name: ");
nameValidation(record[personCount]->name);

printf("Phone_number: ");
phoneNumValidation(record[personCount]->phoneNumber);

printf("Birth: ");
birthDateValidation(record[personCount]->birthDate);

personCount++;
printf("<<%d>>\n", personCount);
}

void showAll() {
for (int i = 0; i < personCount; i++) {
    
    printf("%s %s %s\n", record[i]->name, record[i]->phoneNumber, record[i]->birthDate);
}
}

void deleteFunction() {
char name[20];
printf("Name: ");
nameValidation(name);
for (int i = 0; i < personCount; i++) {
    
    if (strcmp(name, record[i]->name) == 0) {
        for (int j = i; j < personCount; j++) {
            
            
            strcpy(record[j]->name, record[j + 1]->name);
            strcpy(record[j]->phoneNumber, record[j + 1]->phoneNumber);
            strcpy(record[j]->birthDate, record[j + 1]->birthDate);
        }
        personCount--;
        return;
    }
}
}

void findByBirth() {
char birthMonth[3], getBirthMonth[3];
int position = 5, length = 2, check = 0;

printf("Birth month: ");
birthDateValidation(birthMonth);
for (int i = 0; i < personCount; i++) {
    while (check < length) {
        
        getBirthMonth[check] = record[i]->birthDate[position + check - 1];
        check++;
    }
    if (strncmp(birthMonth, getBirthMonth, 2) == 0) {
        
        printf("%s %s %s\n", record[i]->name, record[i]->phoneNumber, record[i]->birthDate);
    }
}

}

int main() {
int menuNum, max_num;

printf("Max_num: ");
scanf("%d", &max_num);

do {
    printf("*****Menu*****\n");
    printf("<1.Registration><2.ShowAll><3.Delete><4.FindByBirth><5.Exit>\n");

    menuNum = choiceValidation("Enter the menu number: ", 1, 5);

    switch (menuNum) {
    case 1:
        if(personCount < max_num)
        {
            registration(); 
            personCount++;
            break;
        }
        else
        {
            printf("OVERFLOW\n");
            break;
        }
    case 2:showAll(); break;
    case 3:deleteFunction(); break;
    case 4:findByBirth(); break;
    case 5:exit(-1); break;
    }
} while (menuNum != 5);

return 0;
}

最新的代码,我编辑使用建议@itati。我设法输入姓名,电话号码和生日。但我只能输入一次,即使我设置的max_num为3,程序停止后,立即输入生日。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

struct PERSON {
char *name;
char *phoneNumber;
char *birthDate;
};

struct PERSON **record;
int personCount = 0;

char nameValidation(char name[]) {
rewind(stdin);
scanf("%s", name);

for (int i = 0; i < strlen(name); i++) {
    if (strlen(name) > 20) {
        printf("Name must be less than 20 characters. Please try again\n");
        printf("Name: ");
        rewind(stdin);
        return nameValidation(name);
    }
    if ((name[i] < 'a' || name[i] > 'z') && (name[i] < 'A' || name[i] > 'Z')) {
        printf("Invalid name. Please try again.\n");
        printf("Name: ");
        rewind(stdin);
        return nameValidation(name);
    }
}
return *name;
}
char phoneNumValidation(char phoneNumber[]) {
rewind(stdin);
scanf("%s", phoneNumber);

for (int i = 0; i < strlen(phoneNumber); i++) {
    if (strlen(phoneNumber) > 15) {
        printf("Phone number must be less than 15 characters. Please try again\n");
        printf("Phone number: ");
        rewind(stdin);
        return phoneNumValidation(phoneNumber);
    }
    if (phoneNumber[i] < '0' || phoneNumber[i] > '9') {
        printf("Invalid phone number. Please try again.\n");
        printf("Phone number: ");
        rewind(stdin);
        return phoneNumValidation(phoneNumber);
    }
}
return *phoneNumber;
}
char birthDateValidation(char birthDate[]) {
rewind(stdin);
scanf("%s", birthDate);

for (int i = 0; i < strlen(birthDate); i++) {
    if (strlen(birthDate) > 8) {
        printf("Birth date must be less than 8 characters. Please try again\n");
        printf("Birth date: ");
        rewind(stdin);
        return birthDateValidation(birthDate);
    }
    if (birthDate[i] < '0' || birthDate[i] > '9') {
        printf("Invalid birth date. Please try again.\n");
        printf("Birth date: ");
        rewind(stdin);
        return birthDateValidation(birthDate);
    }
}
return *birthDate;
}
int choiceValidation(char statement[], int min, int max) {
    int choice;
    printf("%s", statement);
    rewind(stdin);
    scanf("%d", &choice);
    if (choice < min || choice > max) {
        printf("Invalid choice. Please try again.\n");
        return choiceValidation(statement, min, max);
    }
    return choice;
}

void registration(int max_num) {

record = (struct PERSON**) malloc(max_num* sizeof(struct PERSON**));           
while (true){
printf("Name: ");
nameValidation(record[personCount]->name);

printf("Phone_number: ");
phoneNumValidation(record[personCount]->phoneNumber);

printf("Birth: ");
birthDateValidation(record[personCount]->birthDate);

personCount++;
printf("<<%d>>\n", personCount);
}
}



void showAll() {
for (int i = 0; i < personCount; i++) {
    //printf("%s %s %s\n", record[i].name, record[i].phoneNumber, record[i].birthDate);
    printf("%s %s %s\n", record[i]->name, record[i]->phoneNumber, record[i]->birthDate);
}
}

void deleteFunction() {
char name[20];
printf("Name: ");
nameValidation(name);
for (int i = 0; i < personCount; i++) {
    //if (strcmp(name, record[i].name) == 0) {
    if (strcmp(name, record[i]->name) == 0) {
        for (int j = i; j < personCount; j++) {
            //strcpy(record[j].name, record[j + 1].name);
            //strcpy(record[j].phoneNumber, record[j + 1].phoneNumber);
            //strcpy(record[j].birthDate, record[j + 1].birthDate);
            
            strcpy(record[j]->name, record[j + 1]->name);
            strcpy(record[j]->phoneNumber, record[j + 1]->phoneNumber);
            strcpy(record[j]->birthDate, record[j + 1]->birthDate);
        }
        personCount--;
        return;
    }
}
}

void findByBirth() {
char birthMonth[3], getBirthMonth[3];
int position = 5, length = 2, check = 0;

printf("Birth month: ");
birthDateValidation(birthMonth);
for (int i = 0; i < personCount; i++) {
    while (check < length) {
        //getBirthMonth[check] = record[i].birthDate[position + check - 1];
        getBirthMonth[check] = record[i]->birthDate[position + check - 1];
        check++;
    }
    if (strncmp(birthMonth, getBirthMonth, 2) == 0) {
        //printf("%s %s %s\n", record[i].name, record[i].phoneNumber, record[i].birthDate);
        printf("%s %s %s\n", record[i]->name, record[i]->phoneNumber, record[i]->birthDate);
    }
}

}

int main() {
int menuNum, max_num;

printf("Max_num: ");
scanf("%d", &max_num);

do {
    printf("*****Menu*****\n");
    printf("<1.Registration><2.ShowAll><3.Delete><4.FindByBirth><5.Exit>\n");

    menuNum = choiceValidation("Enter the menu number: ", 1, 5);

    switch (menuNum) {
    case 1:
        if(personCount < max_num)
        {
            registration(max_num); 
            personCount++;
            break;
        }
        else
        {
            printf("OVERFLOW\n");
            break;
        }
    case 2:showAll(); break;
    case 3:deleteFunction(); break;
    case 4:findByBirth(); break;
    case 5:exit(-1); break;
    }
} while (menuNum != 5);

return 0;
}
qvtsj1bj

qvtsj1bj1#

我不知道为什么要使用flushPERSON**
根据问题,也许你把“loop”写错了地方。
我重写了一个代码,应该没问题。

void registration(int n) 
{
    
    record = (struct PERSON*) malloc(n* sizeof(struct PERSON*));           
    while (true){
      printf("Name: ");
      scanf("%s", record[personCount]->name);           
    
          printf("Phone Number: ");
      scanf("%s", record[personCount]->phoneNumber);
    
      personCount++;
      printf("<<%d>>\n", personCount);
    }
}

相关问题