美好的一天。我的程序有点问题。该计划应该向俱乐部添加成员,或者根据他们选择的选项将其从俱乐部中移除。该人的信息将存储在一个文件中。但是,我的问题是如果他们选择该选项,如何从文件中删除此人和他/她的信息。案例2将处理该程序的这一部分。我只是提示他们输入他们的名字,因为我知道会有一些比较。我非常感谢能帮助我解决这个问题的人。提前致谢。以下是该计划的代码:
#include <stdio.h>
#include <conio.h>
#include <errno.h>
int PrintMenu();
int PrintMenu(){
printf ("=============================================\n");
printf (" 4-H Membership Menu\n");
printf ("=============================================\n\n");
printf ("Choose which option you want to operate.\n");
printf ("1. Become a member of the club.\n");
printf ("2. Leave the club.");
printf("\n\n");
printf("=============================================\n\n");
}
int CaseFunc();
int CaseFunc(){
FILE *clubFile;
char *fname[28];
char *lname[28];
int age;
int menuANS;
char ans;
char ans1;
int x;
printf("Enter your answer here: ");
scanf(" %d", &menuANS);
switch (menuANS){
case 1:
system("cls");
printf ("Enter your first name: ");
scanf ("%s", fname);
printf("\n");
printf ("Enter your last name: ");
scanf ("%s", lname);
printf("\n");
printf ("Enter your age: ");
scanf ("%d", &age);
system("cls");
Next:
printf ("A memebership fee of $1000 is to be paid to become a member.\n");
printf ("Do you want to continue signing up?\n");
printf ("[y/n only]: ");
scanf (" %c", &ans);
system("cls");
if (ans == 'y'){
printf ("Are you sure? Information will be updated to the system.\n[Note: A fee of $1000 is to be paid.]\n");
printf ("[y/n only]: ");
scanf (" %c", &ans1);
if (ans1 == 'y'){
if ((clubFile = fopen("4-H Information.txt", "a+")) == NULL){ //If the file path is incorrect, an error message is displayed
fprintf(stderr, "Error while opening file (%d: %s)\n",errno, strerror(errno)); //Error message that will be displayed if file path is incorrect
return;
}
fprintf (clubFile, "First Name Last Name Age\n");
fprintf (clubFile, "%s %s %d", fname, lname, age);
fclose(clubFile);
printf("\n\n");
printf ("*You are now a member of the 4-H society.*\n");
}
else if (ans1 == 'n'){
system("cls");
main();
}
}
else if (ans == 'n'){
printf ("Are you sure? All your information will be lost.\n");
printf ("[y/n only]: ");
scanf(" %c", &ans1);
if (ans1 == 'y'){
system("cls");
main();
}
else if (ans1 == 'n'){
system("cls");
goto Next;
}
}
break;
case 2:
printf("Enter your first name: ");
scanf("%s", fname);
printf("\n");
printf("Enter your last name");
scanf("%s", lname);
break;
default:
printf ("Invalid entry of menu option.");
break;
}
}
int main (void){
PrintMenu();
CaseFunc();
getch();
return 0;
}