[英]String input scanf for struct
This is the question consisting of 1st stage, 2nd stage and 3rd stage. 这是由第 1 阶段、第 2 阶段和第 3 阶段组成的问题。
This is the code for 1st stage and it works well.这是第一阶段的代码,效果很好。
#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;
}
And this is the code for 2nd stage where I modified according to the question but I encountered error.这是我根据问题修改但遇到错误的第二阶段代码。
#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;
}
I received error messages when compiling, so I follow the error messages where I need to change all the.我在编译时收到错误消息,所以我按照错误消息我需要更改所有。 to ->.至 ->。
And it can be compiled and run.并且可以编译运行。
I manage to input max_num and menuNum.我设法输入 max_num 和 menuNum。
When I input menuNum 1 (void registration), I try to input the name, but the program stops immediately.当我输入 menuNum 1(无效注册)时,我尝试输入名称,但程序立即停止。
Where did I go wrong?我go哪里错了?
This is the code after I change all the.这是我全部改完后的代码。 to ->至 ->
#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;
}
Latest code I edit using suggestion by @itati.我使用@itati 的建议编辑的最新代码。 I manage to input name, phone number and birthdate.我设法输入姓名、电话号码和生日。 But I can only input once even though I set the max_num to 3, and the program stops immediately after input birthdate.但是即使我将 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;
}
I don't know why you need to use flush
and PERSON**
.我不知道你为什么需要使用flush
和PERSON**
。
According to the problem, maybe you wrote "loop" in the wrong place.根据问题,可能你把“loop”写错了地方。
I rewrite a code, it should be ok.我重写了一段代码,应该可以了。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.