C语言一个通讯录的程序

一个通讯录的程序
怎么写一个删除联系人的功能


//姓名,电话1,电话2,住址,分组 
#include <stdio.h>
#include <string.h>

struct contact {
    char name[50];
    char phone1[20];
    char phone2[20];
    char home[50];
};//创建一个结构体 

int main() {
    struct contact contacts[100];
    int numContacts = 0;//记录人数 
    int choice = 0;

    while (choice != 5) {
        printf("Menu:\n");
        printf("1. 添加\n");
        printf("2. 浏览\n");
        printf("3. 查找\n");
        printf("4. 删除\n");
        printf("5. 退出\n");
        printf("请选择:");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter name: ");
                scanf("%s", contacts[numContacts].name);
                printf("Enter phone1: ");
                scanf("%s", contacts[numContacts].phone1);
                printf("Enter phone2: ");
                scanf("%s", contacts[numContacts].phone2);
                printf("Enter home: ");
                scanf("%s", contacts[numContacts].home);
                numContacts++;
                break;
            case 2:
                if (numContacts == 0) {
                    printf("无\n");
                } else {
                    printf("成员:\n");
                    for (int i = 0; i < numContacts; i++) {
                        printf("%s\t%s\t%s\t%s\n", contacts[i].name, contacts[i].phone1, contacts[i].phone2, contacts[i].home);
                    }
                }
                break;
            case 3:
                if (numContacts == 0) {
                    printf("无\n");
                } else {
                    char search[50];
                    printf("请选择您所查找的对象: ");
                    scanf("%s", search);
                    printf("Search results:\n");
                    for (int i = 0; i < numContacts; i++) {
                        if (strstr(contacts[i].name, search) != NULL || strstr(contacts[i].phone1, search) != NULL || strstr(contacts[i].phone2, search) != NULL || strstr(contacts[i].home, search) != NULL) {
                            printf("%s\t%s\t%s\t%s\n", contacts[i].name, contacts[i].phone1, contacts[i].phone2, contacts[i].home);
                        }
                    }
                }
                break;
//            case 4:
//                 
            case 5:
                printf("Goodbye!\n");
                break;
            default:
                printf("选择错误\n");
                break;
        }
    }

    return 0;
}

不一定对,仅供参考:


//姓名,电话1,电话2,住址,分组
#include <stdio.h>
#include <string.h>

struct contact {
    char name[50];
    char phone1[20];
    char phone2[20];
    char home[50];
};//创建一个结构体

int main() {
    struct contact contacts[100];
    int numContacts = 0;//记录人数
    int choice = 0;

    while (choice != 5) {
        printf("Menu:\n");
        printf("1. 添加\n");
        printf("2. 浏览\n");
        printf("3. 查找\n");
        printf("4. 删除\n");
        printf("5. 退出\n");
        printf("请选择:");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter name: ");
                scanf("%s", contacts[numContacts].name);
                printf("Enter phone1: ");
                scanf("%s", contacts[numContacts].phone1);
                printf("Enter phone2: ");
                scanf("%s", contacts[numContacts].phone2);
                printf("Enter home: ");
                scanf("%s", contacts[numContacts].home);
                numContacts++;
                break;
            case 2:
                if (numContacts == 0) {
                    printf("无\n");
                } else {
                    printf("成员:\n");
                    for (int i = 0; i < numContacts; i++) {
                        printf("%s\t%s\t%s\t%s\n", contacts[i].name, contacts[i].phone1, contacts[i].phone2, contacts[i].home);
                    }
                }
                break;
            case 3:
                if (numContacts == 0) {
                    printf("无\n");
                } else {
                    char search[50];
                    printf("请选择您所查找的对象: ");
                    scanf("%s", search);
                    printf("Search results:\n");
                    for (int i = 0; i < numContacts; i++) {
                        if (strstr(contacts[i].name, search) != NULL || strstr(contacts[i].phone1, search) != NULL || strstr(contacts[i].phone2, search) != NULL || strstr(contacts[i].home, search) != NULL) {
                            printf("%s\t%s\t%s\t%s\n", contacts[i].name, contacts[i].phone1, contacts[i].phone2, contacts[i].home);
                        }
                    }
                }
                break;
            case 4:
                if (numContacts == 0) {
                    printf("无\n");
                } else {
                    int j;
                    printf("请输入你准备删除第几条记录?(1~%d)下面会先显示这条记录,然后让你输入y确认删除: ",numContacts);fflush(stdout);
                    scanf("%d",&j);
                    if (1<=j && j<=numContacts) {
                        j--;
                        printf("%d:  %s\t%s\t%s\t%s\n",j+1, contacts[j].name, contacts[j].phone1, contacts[j].phone2, contacts[j].home);
                        printf("确认要删除这条记录吗?(y/n): ");fflush(stdout);
                        rewind(stdin);
                        char c;
                        scanf("%c",&c);
                        if (c=='y') {
                            if (j<numContacts-1) memmove((void *)(&contacts[j]),(void *)(&contacts[j+1]),sizeof(struct contact)*(numContacts-1));
                            numContacts--;
                        }
                    } else {
                        printf("你输入的记录条数不在(1~%d)范围内!\n",numContacts);
                    }

                }
                break;
            case 5:
                printf("Goodbye!\n");
                break;
            default:
                printf("选择错误\n");
                break;
        }
    }

    return 0;
}

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7444284
  • 这篇博客你也可以参考下:C语言 结构体一个简单的通讯录程序
  • 同时,你还可以查看手册:c语言-成员访问与间接 中的内容
  • 除此之外, 这篇博客: C语言优雅的写一个控制台菜单程序中的 优雅的写一个控制台菜单程序 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include <stdio.h> 
    #include <stdlib.h> 
    
     typedef struct CmdEntry{ 
    	 void (*pfuncmd)(); 
    	 char cHelp[64]; 
     }CmdEntry;
     
      void CreateFile() 
     { 
     	printf("新建文件夹\n"); 
     } 
     
     void OpenFile() 
     { 
     	printf("打开文件\n"); 
     } 
     
     void SaveFile() 
     { 
     	printf("保存文件\n"); 
     } 
     
     void Exit() 
     { 
     	printf("退出\n"); 
     	exit(0); 
     }
     static CmdEntry cmdArray[10] = { 
    	 {&CreateFile, "新建文件夹"}, 
    	 {&OpenFile, "打开文件"}, 
    	 {&SaveFile, "保存文件"}, 
    	 {&Exit, "退出"}, 
    
    	 {0, 0} // ??
     };
     
     void showHelp() 
    { 
    	int i ; 
     	for ( i = 0; (i < 10) && cmdArray[i].pfuncmd; i++)
    	 { 
    		 printf("%d\t%s\n", i, cmdArray[i].cHelp); 
    	 } 
     } 
     
     int main(void) 
     { 
     int iCmdNum; 
     char cTmp1[256]; 
     
     while (1)
     { 
    	 showHelp(); 
    	 printf("请选择\n"); 
    	 iCmdNum = getchar() - '0'; // ????????????????
    	 gets(cTmp1); // ?オ???
    	 if (iCmdNum >= 0 && iCmdNum < 10 && cmdArray[iCmdNum].pfuncmd)
    		 { 
    		 	cmdArray[iCmdNum].pfuncmd(); 
    		 }
    	 else
    		 { 
    		 	printf("对不起,你选的数字不存在,请重新选择\n"); 
    		 } 
    	 } 
    	 return 0; 
      }
    
  • 您还可以看一下 杨波老师的C语言编程初级入门课程中的 第一个程序的编写过程小节, 巩固相关知识点