1

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.)

4
  • Please: indent the arguments to 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. Commented Dec 6, 2013 at 6:07
  • 1
    Print integer in decimal by %d instead of %i. Commented Dec 6, 2013 at 6:09
  • 1
    You may also like %02d for 2-digit hour and min instead of digit extraction manually. Commented Dec 6, 2013 at 6:10
  • 2
    %i is permitted in C99 as an alternative way of printing a decimal integer, primarily for symmetry with scanf(). Commented Dec 6, 2013 at 6:11

2 Answers 2

2

You need an array into which to read the choices:

int choice[10] = { 0 };

You need to use that array in the scanf():

if (scanf("%i", &choice[i]) != 1)
    ...oops...problem reading...

You need to use that array in the printing loop:

for (i = 0; i < num; i++)
{
    j = choice[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);
}

Note the formatting of the printf() statement. The toothbrush style of layout leads to unreadable code. I'd probably use more spaces still in the argument list.

Sign up to request clarification or add additional context in comments.

6 Comments

I like how you added a new variable to keep the array. Would definitely help me with the rest of the sections that I plan to do with the program. However, I don't know if the line where you did the if-statement was meant to be there since you did the oops or if you made an edit and just wanted me to scanf("%i",choice[i]); and follow with the rest of your suggestions. Also sorry for the format. My professor only taught me to "use 3 spaces after each brace" and when it came for a long printf argument I didn't know what to really do.
Many people on SO use 4 spaces per level of indentation; occasionally you get 3 or 8, and some places require 2 spaces per level (where I work does, for example — ick!). The if (scanf(...)) is supposed to be where you currently say just scanf(). You should always check that it managed to convert as many fields as you expected; if it doesn't, you've got problems and need to write some error handling code, probably terminating the loop or even the program, or flushing the unprocessed data, or ... Basically, you need to handle the situation where the user types 'PHYS4' instead of 4.
Ah. When I was reading your line, I was assuming that after the if-statement it would lead into the for loop. (As in if the user typed any number other than 1 it would head into the for loop, and if they chose 1, I would have to put an error. I didn't think about the "PHYS4" situation because of how I said to use the index to choose a class. (Probably should word that a bit clearer in my printf statement.)
Assume user's will make mistakes; they will. Even you will make mistakes.
+1 for the "toothbrush style of layout" comment. That's a new one for me.
|
0

I think I figured out your problem. All i did was redifine J to be an array

   int j[10];

Then changed this block of code

    i,class[j].name,class[j].day,.... ect

to

    class[j[i]].name,class[j[i]].day, ....ect

worked for me. Let me know

2 Comments

Wow, that's interesting. I didn't know you can put an array into an array. I was taught in class to have is separate (for 2d array and so forth). Thank s a lot worked for me. I tried to do something similar before, but I had it as if it was doing a 2d array (I think that's the right terminology for doing it like j[][].)
@Kesakor This is not "an array into an array". With int j[10];, j[i] is an int and class[j[i]].... is simply class[some_integer]..... Now int j[10];, class[j].... would be "an array into an array", which does not work in C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.