下面的程序在退出DisplayTable()函数之后总是转到Suggest()函数中的printf("%s - %d : %d members is busy",NameOfDay[best_day-1],best_period,busy_members);那一句上,谁见过这样的问题?(在TC3上)#include <stdlib.h>
#include <stdio.h>
#include <string.h>int operation;
char NameOfDay[5][10]={"Monday","Tuesday","Wednesday","Thursday","Friday"};void ShowMenu()
{
int choice; // Show the Main Menu
do
{
printf("1. Create/Modify TimeTable\n");
printf("2. Display TimeTable\n");
printf("3. Inquiry How Many Students Will Be Busy At A Specified Time\n");
printf("4. Suggest A Time For Activity\n");
printf("0. Exit Program\n");
printf("\n");
printf("Please enter a choice:");
scanf("%d",&choice);
}while(choice<0 || choice>4);
operation=choice;
return;
}void CreateTable()
{
char index[3];
int TimeTable[5][6],i,j;
FILE *fp; // enter the no. of the student,
// because there are 57 students in my class ,
// the no. should be between 1 and 57
do
{
printf("Please enter the No. of the student (enter 0 to exit) : ");
scanf("%s",&index);
}while(atoi(index)<0 || atoi(index)>57);
if (atoi(index)==0) return; // input whether the student has lesson at a specified day
printf("Enter 1 if the student has lesson at a specified time, and 0 if he doesn't has lesson\n");
for (i=0;i<5;i++)
for (j=0;j<6;j++)
do
{
printf("%s - %d : ",NameOfDay[i],j+1);
scanf("%d",&TimeTable[i][j]);
}while(TimeTable[i][j]!=0 && TimeTable[i][j]!=1); // save data to file
fp=fopen(strcat(index,".dat"),"w");
for (i=0;i<5;i++)
{
for (j=0;j<6;j++)
fprintf(fp,"%3d",TimeTable[i][j]);
fprintf(fp,"\n");
}
fclose(fp);
}
void DisplayTable()
{
char index[3];
int TimeTable;
FILE *fp; // enter the no. of the student
do
{
printf("Please Enter the No. of the Student : ");
scanf("%s",&index);
}while(atoi(index)<0 || atoi(index)>57);
if (atoi(index)==0) return; // Display data
if ((fp=fopen(strcat(index,".dat"),"r"))==NULL)
printf("Cannot display, the student hasn't haven a TimeTable.\n");
else
{
for (int i=0;i<5;i++)
{
for (int j=0;j<6;j++)
{
fscanf(fp,"%3d",&TimeTable);
printf("%3d",TimeTable);
}
printf("\n");
}
fclose(fp);
}
}void Inquiry()
{
FILE *fp;
int i,j,k,busy,total_member=0,busy_members=0,day,period;
char index[3]; // enter the day and period that the user want to Inquiry
do
{
for (i=0;i<5;i++)
printf("%d. %s\n",i+1,NameOfDay[i]);
printf("Please enter the day you want to inquiry: ");
scanf("%d",&day);
}while(day<1 || day>5);
do
{
printf("Please enter the period you want to inquiry: ");
scanf("%d",&period);
}while(period<1 || period>6); //stat
for (i=1;i<=57;i++)
{
if(i<10)
{
index[0]=i+'0';
index[1]='\0';
}
else
{
index[1]=i+'0';
index[0]=i/10+'0';
index[2]='\0';
}
if(!((fp=fopen(strcat(index,".dat"),"r"))==NULL))
{
total_member++;
for (j=0;j<day;j++)
for (k=0;k<period;k++)
fscanf(fp,"%3d",&busy);
if (busy==1) busy_members++;
fclose(fp);
}
} // display info
printf("%d busy members in all %d members\n",busy_members,total_member);}void Suggest()
{
int busy_Table[5][6],day,period,i,best_day,best_period,busy_members=58,busy,total_member=0;
char index[3];
FILE *fp; // stat
for(i=1;i<=57;i++)
{
if(i<10)
{
index[0]=i+'0';
index[1]='\0';
}
else
{
index[1]=i+'0';
index[0]=i/10+'0';
index[2]='\0';
}
if(!(fp=fopen(strcat(index,".dat"),"r"))==NULL)
{
for (day=1;day<=5;day++)
for (period=1;period<=6;period++)
{
fscanf(fp,"%3d",&busy);
busy_Table[day-1][period-1]=busy;
}
total_member++;
fclose(fp);
}
} // display
printf("List of the best periods (Total Members:%d):\n",total_member);
for(i=0;i<10;i++)
{
for (day=1;day<=5;day++)
for (period=1;period<=6;period++)
{
if (busy_Table[day-1][period-1]<busy_members)
{
busy_members=busy_Table[day-1][period-1];
best_day=day;
best_period=period;
}
}
printf("%s - %d : %d members is busy",NameOfDay[best_day-1],best_period,busy_members);
busy_Table[best_day-1][best_period-1]=58;
}
}int main()
{
printf("Welcome to this program!\n\n");
while (1)
{
ShowMenu();
switch (operation)
{
case 1:CreateTable();break;
case 2:DisplayTable();break;
case 3:Inquiry();break;
case 4:Suggest();break;
default:return 0;
}
}
}
#include <stdio.h>
#include <string.h>int operation;
char NameOfDay[5][10]={"Monday","Tuesday","Wednesday","Thursday","Friday"};void ShowMenu()
{
int choice; // Show the Main Menu
do
{
printf("1. Create/Modify TimeTable\n");
printf("2. Display TimeTable\n");
printf("3. Inquiry How Many Students Will Be Busy At A Specified Time\n");
printf("4. Suggest A Time For Activity\n");
printf("0. Exit Program\n");
printf("\n");
printf("Please enter a choice:");
scanf("%d",&choice);
}while(choice<0 || choice>4);
operation=choice;
return;
}void CreateTable()
{
char index[3];
int TimeTable[5][6],i,j;
FILE *fp; // enter the no. of the student,
// because there are 57 students in my class ,
// the no. should be between 1 and 57
do
{
printf("Please enter the No. of the student (enter 0 to exit) : ");
scanf("%s",&index);
}while(atoi(index)<0 || atoi(index)>57);
if (atoi(index)==0) return; // input whether the student has lesson at a specified day
printf("Enter 1 if the student has lesson at a specified time, and 0 if he doesn't has lesson\n");
for (i=0;i<5;i++)
for (j=0;j<6;j++)
do
{
printf("%s - %d : ",NameOfDay[i],j+1);
scanf("%d",&TimeTable[i][j]);
}while(TimeTable[i][j]!=0 && TimeTable[i][j]!=1); // save data to file
fp=fopen(strcat(index,".dat"),"w");
for (i=0;i<5;i++)
{
for (j=0;j<6;j++)
fprintf(fp,"%3d",TimeTable[i][j]);
fprintf(fp,"\n");
}
fclose(fp);
}
void DisplayTable()
{
char index[3];
int TimeTable;
FILE *fp; // enter the no. of the student
do
{
printf("Please Enter the No. of the Student : ");
scanf("%s",&index);
}while(atoi(index)<0 || atoi(index)>57);
if (atoi(index)==0) return; // Display data
if ((fp=fopen(strcat(index,".dat"),"r"))==NULL)
printf("Cannot display, the student hasn't haven a TimeTable.\n");
else
{
for (int i=0;i<5;i++)
{
for (int j=0;j<6;j++)
{
fscanf(fp,"%3d",&TimeTable);
printf("%3d",TimeTable);
}
printf("\n");
}
fclose(fp);
}
}void Inquiry()
{
FILE *fp;
int i,j,k,busy,total_member=0,busy_members=0,day,period;
char index[3]; // enter the day and period that the user want to Inquiry
do
{
for (i=0;i<5;i++)
printf("%d. %s\n",i+1,NameOfDay[i]);
printf("Please enter the day you want to inquiry: ");
scanf("%d",&day);
}while(day<1 || day>5);
do
{
printf("Please enter the period you want to inquiry: ");
scanf("%d",&period);
}while(period<1 || period>6); //stat
for (i=1;i<=57;i++)
{
if(i<10)
{
index[0]=i+'0';
index[1]='\0';
}
else
{
index[1]=i+'0';
index[0]=i/10+'0';
index[2]='\0';
}
if(!((fp=fopen(strcat(index,".dat"),"r"))==NULL))
{
total_member++;
for (j=0;j<day;j++)
for (k=0;k<period;k++)
fscanf(fp,"%3d",&busy);
if (busy==1) busy_members++;
fclose(fp);
}
} // display info
printf("%d busy members in all %d members\n",busy_members,total_member);}void Suggest()
{
int busy_Table[5][6],day,period,i,best_day,best_period,busy_members=58,busy,total_member=0;
char index[3];
FILE *fp; // stat
for(i=1;i<=57;i++)
{
if(i<10)
{
index[0]=i+'0';
index[1]='\0';
}
else
{
index[1]=i+'0';
index[0]=i/10+'0';
index[2]='\0';
}
if(!(fp=fopen(strcat(index,".dat"),"r"))==NULL)
{
for (day=1;day<=5;day++)
for (period=1;period<=6;period++)
{
fscanf(fp,"%3d",&busy);
busy_Table[day-1][period-1]=busy;
}
total_member++;
fclose(fp);
}
} // display
printf("List of the best periods (Total Members:%d):\n",total_member);
for(i=0;i<10;i++)
{
for (day=1;day<=5;day++)
for (period=1;period<=6;period++)
{
if (busy_Table[day-1][period-1]<busy_members)
{
busy_members=busy_Table[day-1][period-1];
best_day=day;
best_period=period;
}
}
printf("%s - %d : %d members is busy",NameOfDay[best_day-1],best_period,busy_members);
busy_Table[best_day-1][best_period-1]=58;
}
}int main()
{
printf("Welcome to this program!\n\n");
while (1)
{
ShowMenu();
switch (operation)
{
case 1:CreateTable();break;
case 2:DisplayTable();break;
case 3:Inquiry();break;
case 4:Suggest();break;
default:return 0;
}
}
}
case 2:{ DisplayTable();break;}
case 3:{ Inquiry();break; }
case 4:{ Suggest();break; }
如上加几个大括号试试。
虽然语法上不要求加括号,但我自己曾碰到过不加括号就出错的情况。
printf("Please Enter the No. of the Student : ");
scanf("%s",&index);
是不是输入了什么值造成了ShowMenu的scanf("%d",&choice);在随后被跟着读取了。为什么不单步跟踪呢?
CreateTable()
函数中的变量index,它的长度为3 byte, 因为你有
char index[3];但在CreateTable()中你使用了
fp=fopen(strcat(index,".dat"),"w");strcat()函数使你的index长度远超过了3,进而导致栈的破坏.更改方法:
char index[256];