如下是一个简易通讯录程序,其中Search函数是一个模糊查询函数。按照我现在的写法在执行该函数后无法退回manu,初学者请问该如何修改?
#include
#include
#include
struct Information
{
char name[255];
char number[255];
char email[255];
char department[255];
}Information;
struct Contact_list
{
int sizeofContact;
struct Information context[500];
};
static int SearchNumber (const struct Contact_list *a, char number[255])
{
int i = 0;
for(i = 0; i < a->sizeofContact; i++)
{
if(0 == strcmp(a->context[i].number, number))
{
return i;
}
}
return -1;
}
void Print_contact_list (const struct Contact_list *a) {
int i = 0;
for (i = 0; i < a->sizeofContact; i++)
{
printf("%s\t%s\t%s %s\n",a->context[i].number,a->context[i].email,a->context[i].name,a->context[i].department);
}
}
void Add_a_contact (struct Contact_list *a)
{
getchar();
printf("Name:");
gets(a->context[a->sizeofContact].name);
printf("Number:");
scanf("%s",a->context[a->sizeofContact].number);
printf("Email:");
scanf("%s",a->context[a->sizeofContact].email);
getchar();
printf("Department:");
gets(a->context[a->sizeofContact].department);
a->sizeofContact++;
}
void Modify_a_contact (struct Contact_list *a)
{
char number[255];
printf("Enter phone number to modify:");
scanf("%s", number);
int x = SearchNumber(a, number);
if (x == -1)
{
printf("Entry does not exist.\n");
}
else
{
getchar();
printf("Enter new name (return to keep [%s]): ", a->context[x].name);
scanf("%[^\n]",a->context[x].name);
printf("Enter new number (return to keep [%s]): ", a->context[x].number);
scanf("%s", a->context[x].number);
printf("Enter new email (return to keep [%s]): ", a->context[x].email);
scanf("%s", a->context[x].email);
getchar();
printf("Enter new department (return to keep [%s]): ", a->context[x].email);
scanf("%[^\n]",a->context[x].email);
}
}
void Delete_a_contact (struct Contact_list *a)
{
char number[255];
printf("Enter phone number to delete (return to cancel): ");
scanf("%s", number);
int x = SearchNumber(a, number);
int y = 0;
for(y = x; y < a->sizeofContact-1; y++)
{
a->context[y] = a->context[y + 1];
}
a->sizeofContact--;
printf("Successfully deleted the entry for %s\n", number);
}
void Search (struct Contact_list *a)
{
char search[255];
int x = 0;
printf("Search:");
scanf("%s",search);
while(x <= a->sizeofContact)
{
if (0 != strstr(a->context[x].name, search))
{
printf("%s\t%s\t%s %s\n", a->context[x].number, a->context[x].email, a->context[x].name,
a->context[x].department);
x++;
}
else
{
continue;
}
}
}
void menu()
{
printf("1) Print contact list\n");
printf("2) Add a contact\n");
printf("3) Modify a contact\n");
printf("4) Delete a contact\n");
printf("5) Search\n");
printf("6) Quit\n");
};
int main()
{
int x = 0;
struct Contact_list C;
do {
begin:
menu();
printf("Option:");
scanf("%d",&x);
if(x > 6)
{
printf("Unknown option!\n");
} else {
switch (x) {
case 1:
Print_contact_list(&C);
break;
case 2:
Add_a_contact(&C);
break;
case 3:
Modify_a_contact(&C);
break;
case 4:
Delete_a_contact(&C);
break;
case 5:
Search(&C);
break;
case 6:
return 0;
}
}
} while (x);
return 0;
}
Search函数:
void Search (struct Contact_list *a)
{
char search[255];
int x = 0;
printf("Search:");
scanf("%s",search);
while(x <= a->sizeofContact)
{
if (0 != strstr(a->context[x].name, search))
{
printf("%s\t%s\t%s %s\n", a->context[x].number, a->context[x].email, a->context[x].name,
a->context[x].department);
x++;
}
else
{
continue;
}
}
}
else里面也需要x++;否则找不到符号条件的就死循环了,或者直接把continue改成x++ 就可以了