I'm currently working on a program in which I list a class and have the user decide which class they would like to take. However, I'm running into an issue with using scanf to determine how the program will output the classes chosen.
struct course
{
char name[7];
char day[4];
int hours,houre,mins,mine;
char ap[3];
int credit;
};
int main(void)
{
int s,i,num,cnum,j;
char l[8][3]={{"st"},{"nd"},{"rd"},{"th"},{"th"},{"th"},{"th"},{"th"}};
struct course class[]={{"MATH1","MWF",7,8,30,50,"AM",5},
{"MATH2","MWF",9,10,00,20,"AM",5},
{"CHEM1","MW ",2,6,30,50,"PM",5},
{"PHYS4","TTH",4,6,00,45,"PM",4},
{"ENGR1","M ",9,10,30,20,"AM",1},
{"ENGR2","TTH",10,12,00,15,"PM",3},
{"ENGR3","MW ",11,12,00,15,"PM",3}};
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",
i,class[i].name,class[i].day,
class[i].hours/10,class[i].hours%10,
class[i].mins/10,class[i].mins%10,
class[i].houre/10,class[i].houre%10,
class[i].mine/10,class[i].mine%10,
class[i].ap,class[i].credit);
}
printf("How many classes would you like to take?:\n");
scanf("%i",&num);
for(i=0;i<num;i++)
{
printf("Select the %i%s class using the index:\n",i+1,l[i]);
scanf("%i",&j);
}
for(i=0;i<num;i++)
printf(" %i %s %s %i%i:%i%i-%i%i:%i%i%s %i\n",
i,class[j].name,class[j].day,
class[j].hours/10,class[j].hours%10,
class[j].mins/10,class[j].mins%10,
class[j].houre/10,class[j].houre%10,
class[j].mine/10,class[j].mine%10,
class[j].ap,class[j].credit);
return 0;
}
Every time I'm done with the scanf it just prints out the last value chosen (to be expected since I'm just reassigning the variable to a number) so I wondered if anyone would know how to alleviate the problem. Sorry about the style of the code, I didn't' want to lost track of the arrays when I was making the printf statements. And the reasons why I have spaces between the %s's and %i's has to do with how the program will look "good" with a table presented with it. (You might have to run the code in order to maybe fully know what I'm trying to do. Also the reason why I didn't put the last printf with the previous for-loop is due to how I wanted the program to print out the schedule table after the user has inputted all the classes they would want to take.)
printf()less deeply. Leave the format string on line one, and indent the arguments on the remaining lines at the same level as the start of the format string.%dinstead of%i.%02dfor 2-digit hour and min instead of digit extraction manually.%iis permitted in C99 as an alternative way of printing a decimal integer, primarily for symmetry withscanf().