c - 替换数组中的数据时出现问题

标签 c arrays for-loop multidimensional-array scanf

对于我必须做的项目,我必须列出一组类(class),让用户选择要使用的类(class),并打印出他们该学期的每周时间表。 (与我问的第一个问题相同的程序。)但是,当我尝试打印每周的时间表时,我似乎遇到了问题。 (该程序相当冗长,至少以我在 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 您想要参加的类(class)数量,并使用 0 和 1 作为类(class)选择的索引。 (我没有任何 if 语句以及诸如此类的东西来考虑用户可能选择的其他类。) 这是我正在尝试为我的程序执行的操作的照片。 http://postimg.org/image/3tlgtwu9h/我使用油漆将时间表放入方框中,以便在编码时直观地看到不同的数组。

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

最佳答案

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

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

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

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

char * a = "MTW"; char *b = a 然后 a == b 将为 true

关于c - 替换数组中的数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20507239/

相关文章:

c - 在c中反转给定指针的字节的函数

c - 如何制作结构数组

c++ - POSIX regex.h 是否提供 unicode 或基本上非 ascii 字符?

c - 如何在 C 中将字符数组与整数进行异或

c - while循环中的if语句不起作用

javascript - 随机清空 Javascript 数组

javascript - 匹配对象数组中的 2 个属性值,并根据第三个属性值过滤匹配

c++ - 为什么要比较 Unsigned Int >= 0 a "Pointless Comparison"?

cat/Xargs/command VS for/bash/command

python - 在 "for line in data"循环中使用 .readline() 将文件的多行分配给多个变量