在替换数组中的数据时遇到问题

时间:2013-12-10 23:05:25

标签: c arrays for-loop multidimensional-array scanf

对于我必须要做的项目,我必须列出一组课程,让用户选择要使用的课程,并为学期打印每周课程。 (与我提出的第一个问题相同的程序。)然而,当我尝试打印每周时间表时,我似乎遇到了问题。 (该计划相当冗长,至少根据我在C中的经验。)

struct course
{
 int index;
 char name[7];
 char day[4];
 int hours,houre,mins,mine;
 char ap[3];
 int credit;
};
struct course whcl[]={ {0,"MATH1","MWF",7,8,30,50,"AM",5},
                       {1,"MATH2","MWF",9,10,00,20,"AM",5},
                       {2,"CHEM1","MW ",2,6,30,50,"PM",5},
                       {3,"PHYS4","TTH",4,6,00,45,"PM",4},
                       {4,"ENGR1","M  ",9,10,30,20,"AM",1},
                       {5,"ENGR2","TTH",10,12,00,15,"PM",3},
                       {6,"ENGR3","MW ",11,12,00,15,"PM",3}};
int choice[15],i,j,k,num,z,s;
void printout(int z); //(To be put in when I fix the function)

int main(void)
{
 char l[8][3]={{"st"},{"nd"},{"rd"},{"th"},{"th"},{"th"},{"th"},{"th"}};
 printf("                 Fall Schedule\n");
 printf("Index  Course  Day        Time       Credit\n");
 printf("-------------------------------------------\n");
 for(i=0;i<7;i++)
 {
  printf("  %i    %s  %s    %i%i:%i%i-%i%i:%i%i%s     %i\n",
          whcl[i].index,whcl[i].name,whcl[i].day,
          whcl[i].hours/10,whcl[i].hours%10,
          whcl[i].mins/10,whcl[i].mins%10,
          whcl[i].houre/10,whcl[i].houre%10,
          whcl[i].mine/10,whcl[i].mine%10,
          whcl[i].ap,whcl[i].credit);
 }
 printf("How many classes would you like to take?: ");
 scanf("%i",&num);
 for(i=0;i<num;i++)
 {
  printf("Select the %i%s class using the index: ",i+1,l[i]);
  scanf("%i",&choice[i]);
 }
 printf("The classes you have selected are:\n");
 printf("Index  Course  Day       Time       Credit\n");
 printf("-------------------------------------------\n");
 for(i=0;i<num;i++)
 {
  s=choice[i];
  printf("  %i    %s   %s   %i%i:%i%i-%i%i:%i%i%s    %i\n",
          whcl[s].index,whcl[s].name,whcl[s].day,
          whcl[s].hours/10,whcl[s].hours%10,
          whcl[s].mins/10,whcl[s].mins%10,
          whcl[s].houre/10,whcl[s].houre%10,
          whcl[s].mine/10,whcl[s].mine%10,
          whcl[s].ap,whcl[s].credit);
 }
 printf("Your weekly schedule for Fall is:\n");
 printf("    Time     Monday   Tuesday   Wednesday   Thursday   Friday\n");
 printout(z);
 return 0;
}

void printout(int z)
{
 int start,starti,end,endi,num;
 int slot[25][6];
 for(i=0;i<24;i++)
  for(j=0;j<5;j++)
   slot[i][j]=99;
 for(i=0;i<num;i++)
 {
  if ((whcl[choice[i]].day)=="MWF")//I think the problem is here.
  {
   start=whcl[choice[i]].hours*60+whcl[choice[i]].mins;
   end=whcl[choice[i]].houre*60+whcl[choice[i]].mine;
   starti=(start-450)/30;
   endi=(end-450)/30;
   for(j=starti;j<=endi;j++)
    slot[j][1]=slot[j][3]=slot[j][6]=whcl[choice[i]].index;
  }
 }
 for(i=0;i<24;i++)
 {
  printf("%i%i:%i%i-%i%i:%i%i ",
         (450+(i-1)*30)/60/10,(450+(i-1)*30)/60%10,
         (450+(i-1)*30)%60/10,(450+(i-1)*30)%60%10,
         (450+(i-1)*30+30)/60/10,(450+(i-1)*30+30)/60%10,
         (450+(i-1)*30+30)%60/10,(450+(i-1)*30+30)%60%10);
  for(j=0;j<4;j++)
  {
   if (slot[i][j]!=99) //Use Note here
    printf(" %s       ",whcl[choice[i]].name);
   else
    printf("");
  }
  printf("\n");
 }
return;
}

当我打印出时间表时,唯一出现的就是时间。其他一切都是空白的。我认为这是由于我试图用99以外的东西替换插槽阵列。如果你打算运行这个程序,请使用2你想要的类数量,并在类选择上使用0和1作为索引。 (我没有任何if语句,也没有考虑用户可能选择的其他类。)这是我正在尝试为我的程序做的照片。 http://postimg.org/image/3tlgtwu9h/我用油漆把时间表放在方框中,以便在编码时直观地看到不同的数组。

注意:如果你将if语句更改为[i] [j] == 99你可以看到桌子上印有“Class”,但是它填满了整个数组插槽,这证实了我的想法,我搞砸了试图替换数组中的数据。另外,我用99填充它以使99与空格相关联。

1 个答案:

答案 0 :(得分:0)

if ((whcl[choice[i]].day)=="MWF") //我认为问题就在这里

正确,您需要使用strcmp来比较字符串,而不是==

尝试: if (strcmp(whcl[choice[i]].day,"MWF") == 0)

等于==将检查指针是否相同,以便您可以:

char * a = "MTW"; char *b = a然后a == b将是真的