Home Forums C Programming How to redo the same code using structs

Viewing 3 reply threads
  • Author
    Posts
    • #2216
      MaricruDeluca
      Participant

      I have this project that i need to redo the exact same way, but using structs. I can get as far as creating the structs with the fields, however, I am having difficulty getting started, I don’t quite have down the concept, or at least i’m having trouble ‘wrapping’ my head around the concept, i just can’t seem to get started; past the declaration.

      any help would be appreciated. Thanks

      #include 
      #include
      #include
      #define SIZE 5
      #define LENGTH 41
      #define COSTPERCREDITHOUR 120.25
      #define ID_FEE 35.00

      //need to do same project using only structs
      typedef struct{
      int id;
      int crn[4];
      int name[40];
      }STUDENT;

      //void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);
      void addStudent(STUDENT []);
      void menu (int *option);
      int linearSearch (int id[SIZE], int key);
      void addDelete (char name[SIZE][LENGTH], int crn[SIZE][4], int location);
      void getInfo (int crn, char prefix[7], int *hours);
      void printInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location);
      void sortInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location);
      //
      int main ()
      {
      char name[SIZE][LENGTH] = {""};
      int id[SIZE] = {0};
      int crn[SIZE][4] = {{0}};
      char invoice;

      int option = 0, loop = 0, key = 0, location = 0;

      while (loop == 0)
      {
      menu (&option);

      switch (option)
      {
      case 1:
      {
      addStudent (name, id, crn);
      break;
      }
      case 2:
      {
      printf ("Enter the students ID: ");
      scanf ("%d", &key);
      location = linearSearch (id, key);
      addDelete (name, crn, location);
      printf ("nWould you like to display a new invoice? (Y/N): ");
      fflush (stdin);
      scanf ("%c", &invoice);
      if (invoice == 'y' || invoice == 'Y')
      {
      printInvoice (id, name, crn, location);
      }
      break;
      }
      case 3:
      {
      printf ("Enter the students ID: ");
      scanf ("%d", &key);
      location = linearSearch (id, key);
      if (location == -1)
      printf ("nNo student found!n");
      else
      printf ("nStudent found @ index location %d.n", location);
      break;
      }
      case 4:
      {
      printf ("Enter the students ID: ");
      scanf ("%d", &key);
      location = linearSearch (id, key);
      printInvoice (id, name, crn, location);
      break;
      }
      case 5:
      {
      printf ("Enter the students ID: ");
      scanf ("%d", &key);
      location = linearSearch (id, key);
      sortInvoice (id, name, crn, location);
      break;
      }
      case 0:
      {
      loop=1;
      break;
      }
      default:
      {
      printf ("Invalid option, try again.n");
      break;
      }

      }
      }

      printf ("Goodbye!nnn");

      system ("pause");
      return 0;
      }/*end main function*/

      void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4])
      {
      char temp[LENGTH];
      int i=0 , n , j;
      int finder;


      printf("Enter the ID: ");
      scanf("%d", &finder);
      while ( i < 4 && finder != id)
      i++;
      if(finder == id
      )
      {
      printf("nThe ID %d is already assigned to a student", finder);
      return;
      }else if (finder != id
      )
      {


      printf ("Enter the name of the student: ");
      //fflush (stdin);
      gets (temp);
      gets (temp);

      for ( i=0; i {
      if ( strcmp (name
      , "") == 0 )
      {
      id
      = finder;
      strcpy ( name
      , temp);
      printf ("Enter how many CRNs %s is taking: ", temp);
      scanf ("%d", &n);
      if (n > 4)
      {
      printf ("nYou cannot take more than 4 classes.n");
      strcpy (name, "");
      id
      = 0;
      return;
      }

      else if (n < 0)
      {
      printf ("nYou must take at least one class.n");
      strcpy (name, "");
      id
      = 0;
      return;
      }

      else
      {
      printf ("Enter the %d CRNs: ", n);
      for (j=0; j {
      scanf ("%d", &crn
      [j]);
      }
      printf ("nStudent added!n");
      break;
      }
      }
      }
      }
      }/*end addStudent function*/


      void menu (int *option)
      {
      printf ("tWelcome!n");
      printf ("Choose from the following options:nn");
      printf("=============================================n");
      printf ("1 - Add a new studentn");
      printf ("2 - Add/delete a coursen");
      printf ("3 - Search for a studentn");
      printf ("4 - Print fee invoicen");
      printf ("5 - Print fee invoice sorted by CRNn");
      printf ("0 - Exit programn");
      printf ("nEnter your selection: ");
      scanf ("%d", option);

      }/*end menu option*/


      int linearSearch (int id[SIZE], int key)
      {
      int i;
      int location = -1;

      for ( i= 0; i {
      if (id
      == key )
      {
      location = i;
      break;
      }
      }

      return location;
      }/*end linearSearch function */

      void addDelete (char name[SIZE][LENGTH], int crn[SIZE][4], int location)
      {
      int i, temp;
      //char invoice;
      char prefix[7];
      int hours;
      char selection;

      printf ("nHere are the courses [%s] is taking: ", name[location]);
      printf ("nCRNtPREFIXtCR HOURS");
      for ( i= 0; i<4; i++)
      {
      if (crn[location]
      != 0)
      {
      getInfo ( crn[location]
      , prefix , &hours);
      printf ("n%dtt%stt%d", crn[location]
      , prefix, hours);
      }
      else
      break;
      }
      printf ("nnChoose from:");
      printf ("nA - Add a new course for [%s]", name[location]);
      printf ("nD - Delete a course from [%s]'s schedule", name[location]);
      printf ("nC - Cancel operation");
      printf ("nEnter your selection here: ");
      fflush (stdin);
      scanf ("%c", &selection);

      if (selection == 'A' || selection == 'a')
      {

      for ( i= 0; i<4; i++)
      {
      if (crn[location]
      == 0)
      {
      printf ("nEnter course number to add: ");
      scanf ("%d", &crn[location]
      );
      getInfo (crn[location]
      , prefix, &hours);
      printf ("n[%d %s] successfully added!n", crn[location]
      , prefix);
      break;
      }

      if (i==3 && crn[location]
      != 0)
      {
      printf ("nYou cannot have more than 4 courses.");
      return;
      }
      }


      }

      else if (selection == 'D' || selection == 'd')
      {
      printf ("nEnter course number to delete: ");
      scanf ("%d", &temp);
      for ( i= 0; i<4; i++)
      {
      if (temp == crn[location]
      )
      {
      getInfo (crn[location]
      , prefix, &hours);
      printf ("n[%d %s] successfully deleted!n", crn[location]
      , prefix);
      crn[location]
      =0;
      while ( i<4)
      {
      crn[location]
      = crn[location][i+1];
      i++;
      }
      break;
      }
      }
      }
      else if (selection == 'C' || selection == 'c')
      {
      return;
      }
      else
      {
      printf ("nInvalid selection.");
      }




      }

      void getInfo (int crn, char prefix[7], int *hours)
      {
      switch (crn)
      {
      case 4587:
      {
      strcpy (prefix, "MAT 236");
      *hours = 4;
      break;
      }
      case 4599:
      {
      strcpy (prefix, "COP 220");
      *hours = 3;
      break;
      }
      case 8997:
      {
      strcpy (prefix, "GOL 124");
      *hours = 1;
      break;
      }
      case 9696:
      {
      strcpy (prefix, "COP 100");
      *hours = 3;
      break;
      }
      }
      }

      void printInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location)
      {

      double total = 0;
      int hours;
      char prefix[7];
      int i;
      double subTotal;

      total = ID_FEE;

      printf ("nnnnttVALENCE COMMUNITY COLLEGE");
      printf ("nttORLANDO FL 10101");
      printf ("ntt*************************");
      printf ("nnttFee Invoice Prepared for Student:");
      printf ("ntt%d - [%s]", id[location], name[location]);
      printf ("nntt1 Credit Hour = $%.2lf", COSTPERCREDITHOUR);
      printf ("nnttCRNtPREFIXt HOURS");
      for ( i= 0; i<4; i++)
      {
      if (crn[location]
      != 0)
      {
      getInfo ( crn[location]
      , prefix , &hours);
      subTotal = hours * COSTPERCREDITHOUR;
      printf ("ntt%dt%stt%dt$%.2lf", crn[location]
      , prefix , hours, subTotal);

      total = total + subTotal;
      }else if (crn[location]
      == 0)
      break;
      }
      printf ("nntttHealth & ID Feest$ %.2lf", ID_FEE);
      printf ("nntt

      ");
      printf ("ntttTotal Paymentstt$%.2lfnn", total);
      }

      void sortInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location)
      {
      double total = 0;
      int hours;
      char prefix[7];
      int i;
      double subTotal;
      int j, temp ;


      for ( j= 0; j {
      for ( i=0; i<=SIZE-2 ; i++)
      {
      if ( crn[location] > crn[location][i+1] && crn[location][i+1] != 0 )
      {
      temp = crn[location]
      ;
      crn[location]
      = crn[location][i+1];
      crn[location][i+1] = temp;
      }
      }
      }


      total = ID_FEE;

      printf ("nnnnttVALENCE COMMUNITY COLLEGE");
      printf ("nttORLANDO FL 10101");
      printf ("ntt*************************");
      printf ("nnttFee Invoice Prepared for Student:");
      printf ("ntt%d - [%s]", id[location], name[location]);
      printf ("nntt1 Credit Hour = $%.2lf", COSTPERCREDITHOUR);
      printf ("nnttCRNtPREFIXt HOURS");
      for ( i= 0; i<4; i++)
      {
      if (crn[location]
      != 0)
      {
      getInfo ( crn[location]
      , prefix , &hours);
      subTotal = hours * COSTPERCREDITHOUR;
      printf ("ntt%dt%stt%dt$%.2lf", crn[location]
      , prefix , hours, subTotal);

      total = total + subTotal;
      }else if (crn[location]
      == 0)
      break;
      }
      printf ("nntttHealth & ID Feest$ %.2lf", ID_FEE);
      printf ("nntt

      ");
      printf ("ntttTotal Paymentstt$%.2lfnn", total);
      }

    • #3596
      GWILouisaxwzkla
      Participant

      You really just need to group all your data together ( structs are handy for this purpose ). So if you have a function prototype like:


      void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);

      change it to:


      void addStudent ( STUDENT newStudent );

      when you need to access a member of the struct , just use the dot operator and use the data as usual:


      ..........
      printf ( newStudent.name ); //output the student name
      printf ( "%i" , newStudent.id ); //output the student id

      not too much to the conversion …….

    • #3597
      MaricruDeluca
      Participant

      Thank you!

    • #3598
      GWILouisaxwzkla
      Participant

      A little correction. It seems you are passing arrays of names , id’s and crn into the function:


      void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);

      so you would change your prototype to :


      const int numberOfStudents = 20; //whatever
      void addStudent ( STUDENT newStudent [ numberOfStudents ] );

      and access the data members like this:


      ..........
      int i = currentStudent;
      printf ( newStudent [ currentStudent ].name ); //output the student name
      printf ( "%i" , newStudent [ currentStudent ] .id ); //output the student id

      my bad ………

Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.