进入第一页或第二页后,返回菜单,只能访问第一页,不能访问第二页。
我试过在每个字符串和字符输入之前放置 fflush(stdin)。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
int password, i, j, timesVisited;
char name[10][15]={"jay"}, address[10][40]={"singapore"}, ic[10][12], gender[10][6], contact[10][11]={"010"}, dataToEdit[8], patientToEdit[20], newData[40], menuOption, returnOption;
while(returnOption!=2){
//Menu
puts("Menara Clinic\n");
puts("Press 1 to add patients' info");
puts("Press 2 to edit patients' info");
puts("Press 3 to access patients' history");
puts("Press 4 to access inventory");
puts("Press 5 to delete patients' info"); fflush(stdin);
scanf("%c", &menuOption);
//menuOption = getch();
system("cls");
//Page 1
if(menuOption=='1')
do
{ for(i=0; i<1; i++) //CHANGE i VALUE TO 10 AFTER TESTING
{ printf("Enter name: "); fflush(stdin);
scanf("%s", name[i]);
printf("Enter gender: "); fflush(stdin);
scanf("%s", gender[i]);
printf("Enter address: "); fflush(stdin);
scanf("%s", address[i]);
printf("Enter contact no: "); fflush(stdin);
scanf("%s", contact[i]);
printf("Enter IC: "); fflush(stdin);
scanf("%s", ic[i]);
putchar('\n');
puts("Press 0 to continue");
puts("Press 1 to return to menu"); fflush(stdin);
returnOption = getch();
}
}while(returnOption=='0');
if(returnOption=='1')
{ system("cls");
continue;
}
//Page 2
else if(menuOption=='2')
{ do{
i=0;
printf("Enter name: "); fflush(stdin);
scanf("%s", patientToEdit);
for(i; i<1; i++) //CHANGE i VALUE TO 10 AFTER TESTING
{ if(strcmp(name[i], patientToEdit)==0)
{ printf("Enter data to edit: "); fflush(stdin);
scanf("%s", dataToEdit);
if(strcmp(dataToEdit,"address")==0)
{ printf("Enter new address: "); fflush(stdin);
scanf("%s", newData);
strcpy(address[i], newData);
printf("%s's new address is now %s\n", name[i], address[i]);
}
else if(strcmp(dataToEdit,"contact")==0)
{ printf("Enter new contact no: "); fflush(stdin);
scanf("%s", newData);
strcpy(contact[i], newData);
printf("%s's new contact no is now %s\n", name[i], contact[i]);
}
}
}
putchar('\n');
puts("Press 0 to continue");
puts("Press 1 to return to menu"); fflush(stdin);
returnOption = getch();
}while(returnOption=='0');
if(returnOption=='1')
{ system("cls");
continue;
}
}
}
}
该程序应该允许您从菜单进入任何页面,即使之前已经访问过一个页面。
最佳答案
在
char name[10][15]={"jay"},..., menuOption, returnOption; while(returnOption!=2){
有两个错误:
- 你错过了初始化returnOption,所以当你第一次测试它时行为是未定义的
- 你想与'2'而不是2进行比较,所以
while(returnOption!='2')
而不是while(returnOption!=2)
还有:
替换
scanf("%c", &menuOption);
通过scanf(" %c", &menuOption);
绕过空格和换行符替换两个
returnOption = getch();
通过scanf(" %c", &returnOption);
并删除所有
fflush(stdin);
因为他们什么都不做,从它的描述中摘录:
For input streams associated with seekable files (e.g., disk files, but not pipes or terminals), fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.
我还强烈建议您通过限制 scanf("%s...
读取的允许字符数来保护数组溢出。 , 例如一个 name 被内存在 15 个字符(包括空字符)上 scanf("%14s", name[i]);
也是如此等等
我不明白为什么
putchar('\n'); puts("Press 0 to continue"); puts("Press 1 to return to menu"); returnOption = getch();
在for(i=0; i<1; i++)
里面虽然您不测试输入值,但您可能想要:
//Page 1
if(menuOption=='1')
{
for(i=0; i<1; i++) //CHANGE i VALUE TO 10 AFTER TESTING
{
printf("Enter name: ");
scanf("%s", name[i]);
printf("Enter gender: ");
scanf("%s", gender[i]);
printf("Enter address: ");
scanf("%s", address[i]);
printf("Enter contact no: ");
scanf("%s", contact[i]);
printf("Enter IC: ");
scanf("%s", ic[i]);
putchar('\n');
puts("enter 1 to return to menu");
if ((scanf(" %c", &returnOption) == 1) &&
(returnOption == '1'))
break;
}
system("cls");
}
//Page 2
else if(menuOption=='2')
请注意,当i 为 9 时,您也可以不要求继续或不继续
关于进入前一页后无法从菜单进入第二页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55765361/