我的程序運行完一個功能後按任意鍵就退出了,而不是返回主菜單
代碼如下,應該怎麼改?(具體一點,菜鳥一個)多謝!
#include
#include
#include
#include
#include
#define LENTEL sizeof(tel)
#define ID struct tel
struct tel
{
char name[15];
char tele[11];
char style[15];
char mail[20];
ID * next;
};
ID *id = NULL;
/*讀取telephone.txt文件中的內容創建學生鏈表*/
ID* create()
{
ID head;
ID *p1,*p2;
p2 = (ID)malloc(LENTEL);
head = p2;
FILE *fp;
if((fp = fopen("telephone.txt","r")) == NULL)
{
printf("聯系人文件打開錯誤,請將telephone.txt放置與程序同一目錄下!\n");
exit(0);
}
while(!feof(fp))
{
p1 = (ID*)malloc(LENTEL);
fscanf(fp,"%s%s%s%s",p1->name,p1->tele,p1->style,p1->mail);
p1->next = NULL;
p2->next = p1;
p2 = p1;
}
fclose(fp);
return head;
}
ID* findbyname(char name[])
{
ID* p = id;
ID* temp = NULL;
while (p!=NULL)
{
if(strcmp(name,p->name) == 0)
{
temp = p;
break;
}
else
{
p = p->next;
}
}
return temp;
}
ID* findbytele(char tele[])
{
ID* p = id;
ID* temp = NULL;
while (p!=NULL)
{
if(strcmp(tele,p->tele) == 0)
{
temp = p;
break;
}
else
{
p = p->next;
}
}
return temp;
}
/*判斷這個聯系人是否存在*/
bool Istel(char name[])
{
ID *p = id->next;
bool flag = false;
while (p != NULL)
{
if(strcmp(p->name,name) == 0)
{
flag = true;
}
p=p->next;
}
return flag;
}
int add()
{
menu();
ID *newtel = NULL;
ID *p = id;
char tel_name[15];
char tel_tele[11];
char tel_style[15];
char tel_mail[20];
printf("\n------- 請輸入學生基本信息 --------\n");
printf("姓名 電話 類別 郵箱\n");
printf("--------------------------------------\n");
scanf("%s%s%s%s",tel_name,tel_tele,tel_style,tel_mail);
if(!Istel(tel_name))
{
newtel = (ID*)malloc(LENTEL);
strcpy(newtel->name,tel_name);
strcpy(newtel->tele,tel_tele);
strcpy(newtel->style,tel_style);
strcpy(newtel->mail,tel_mail);
newtel->next = NULL;
for(p=p->next; p->next!=NULL; p=p->next);
p->next = newtel;
printf("\n聯系人已添加成功!\n");
}
else
{
printf("\n該聯系人姓名已存在!\n");
}
getch();
return 0;
}
void Print_Inquire_All()
{
void menu_print_out();
ID *p1 = id->next;
menu_print_out();
while (p1!=NULL)
{
printf("%s\t%s\t%s\t%s\n",p1->name,p1->tele,p1->style,p1->mail);
p1 = p1->next;
}
system("pause");
}
void menu_print_out()
{
printf("\n---------------------------------\n");
printf("\n---------------------------------\n");
}
/*主菜單*/
void menu(void)
{
void ReadBack();
printf(" ╭════════╮ \n");
printf("╭══════╣通訊管理系統V1.0╠══════╮\n");
printf("║ ╰════════╯ ║\n");
printf("║ 【1】添加數據 【3】退出系統 ║\n");
printf("║ ║\n");
printf("║ 【2】查詢數據 ************* ║\n");
printf("║ ║\n");
printf("╰══════════════════════╯\n");
printf("◎請輸入功能前的序號進入相應的工具:【 】\b\b");
int a = 0;
a = getchar();
while(a!='1'&&a!='2'&&a!='3')
{
printf("error! please input the right number!\n");
putchar('\a');
getchar();
printf("◎請重新輸入功能前的序號進入相應的工具:【 】\b\b");
a = getchar();
}
switch(a)
{
case '1':
add();
break;
case '2':
Print_Inquire_All();
case '3':
ReadBack();
exit(0);
break;
}
getchar();
}
/*退出程序時將數據寫回telephone.txt進行更新*/
void ReadBack()
{
FILE *TEL = fopen("telephone.txt","w");
ID *te = id->next;
while (te != NULL)
{
fwrite((char*)&id,sizeof(ID),1,TEL);
te = te->next;
}
fclose(TEL);
}
void Lunch()
{
id = create();
menu();
}
void main()
{
SetConsoleTitle("C語言通訊錄管理系統");
Lunch();
}
用一個while循環 把主菜單放到循環中 並進行字符判斷例如 回車繼續循環主菜單 輸入exit則退出 最好加一個清屏函數