Home › Forums › C Programming › How to redo the same code using structs
- This topic has 3 replies, 2 voices, and was last updated 15 years, 5 months ago by GWILouisaxwzkla.
- AuthorPosts
- July 26, 2009 at 1:44 pm #2216MaricruDelucaParticipant
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411#include <stdio.h><br />#include <string.h><br />#include <stdlib.h><br />#define SIZE 5<br />#define LENGTH 41<br />#define COSTPERCREDITHOUR 120.25<br />#define ID_FEE 35.00<br /><br />//need to do same project using only structs<br />typedef struct{<br />int id;<br />int crn[4];<br />int name[40];<br />}STUDENT;<br /><br />//void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);<br />void addStudent(STUDENT []);<br />void menu (int *option);<br />int linearSearch (int id[SIZE], int key);<br />void addDelete (char name[SIZE][LENGTH], int crn[SIZE][4], int location);<br />void getInfo (int crn, char prefix[7], int *hours);<br />void printInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location);<br />void sortInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location);<br />//<hr class="bbcode_rule" />int main ()<br />{<br />char name[SIZE][LENGTH] = {""};<br />int id[SIZE] = {0};<br />int crn[SIZE][4] = {{0}};<br />char invoice;<br /><br />int option = 0, loop = 0, key = 0, location = 0;<br /><br />while (loop == 0)<br />{<br />menu (&option);<br /><br />switch (option)<br />{<br />case 1:<br />{<br />addStudent (name, id, crn);<br />break;<br />}<br />case 2:<br />{<br />printf ("Enter the students ID: ");<br />scanf ("%d", &key);<br />location = linearSearch (id, key);<br />addDelete (name, crn, location);<br />printf ("nWould you like to display a new invoice? (Y/N): ");<br />fflush (stdin);<br />scanf ("%c", &invoice);<br />if (invoice == 'y' || invoice == 'Y')<br />{<br />printInvoice (id, name, crn, location);<br />}<br />break;<br />}<br />case 3:<br />{<br />printf ("Enter the students ID: ");<br />scanf ("%d", &key);<br />location = linearSearch (id, key);<br />if (location == -1)<br />printf ("nNo student found!n");<br />else<br />printf ("nStudent found @ index location %d.n", location);<br />break;<br />}<br />case 4:<br />{<br />printf ("Enter the students ID: ");<br />scanf ("%d", &key);<br />location = linearSearch (id, key);<br />printInvoice (id, name, crn, location);<br />break;<br />}<br />case 5:<br />{<br />printf ("Enter the students ID: ");<br />scanf ("%d", &key);<br />location = linearSearch (id, key);<br />sortInvoice (id, name, crn, location);<br />break;<br />}<br />case 0:<br />{<br />loop=1;<br />break;<br />}<br />default:<br />{<br />printf ("Invalid option, try again.n");<br />break;<br />}<br /><br />}<br />}<br /><br />printf ("Goodbye!nnn");<br /><br />system ("pause");<br />return 0;<br />}/*end main function*/<br /><br />void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4])<br />{<br />char temp[LENGTH];<br />int i=0 , n , j;<br />int finder;<br /><br /><br />printf("Enter the ID: ");<br />scanf("%d", &finder);<br />while ( i < 4 && finder != id<i>)<br />i++;<br />if(finder == id</i><i>)<br />{<br />printf("nThe ID %d is already assigned to a student", finder);<br />return;<br />}else if (finder != id</i><i>)<br />{<br /><br /><br />printf ("Enter the name of the student: ");<br />//fflush (stdin);<br />gets (temp);<br />gets (temp);<br /><br />for ( i=0; i<SIZE; i++)<br />{<br />if ( strcmp (name</i><i> , "") == 0 )<br />{<br />id</i><i> = finder;<br />strcpy ( name</i><i>, temp);<br />printf ("Enter how many CRNs %s is taking: ", temp);<br />scanf ("%d", &n);<br />if (n > 4)<br />{<br />printf ("nYou cannot take more than 4 classes.n");<br />strcpy (name, "");<br />id</i><i> = 0;<br />return;<br />}<br /><br />else if (n < 0)<br />{<br />printf ("nYou must take at least one class.n");<br />strcpy (name, "");<br />id</i><i> = 0;<br />return;<br />}<br /><br />else<br />{<br />printf ("Enter the %d CRNs: ", n);<br />for (j=0; j<n ; j++)<br></n>{<br />scanf ("%d", &crn</i><i>[j]);<br />}<br />printf ("nStudent added!n");<br />break;<br />}<br />}<br />}<br />}<br />}/*end addStudent function*/<br /><br /><br />void menu (int *option)<br />{<br />printf ("tWelcome!n");<br />printf ("Choose from the following options:nn");<br />printf("=============================================n");<br />printf ("1 - Add a new studentn");<br />printf ("2 - Add/delete a coursen");<br />printf ("3 - Search for a studentn");<br />printf ("4 - Print fee invoicen");<br />printf ("5 - Print fee invoice sorted by CRNn");<br />printf ("0 - Exit programn");<br />printf ("nEnter your selection: ");<br />scanf ("%d", option);<br /><br />}/*end menu option*/<br /><br /><br />int linearSearch (int id[SIZE], int key)<br />{<br />int i;<br />int location = -1;<br /><br />for ( i= 0; i<SIZE; i++)<br />{<br />if (id</i><i> == key )<br />{<br />location = i;<br />break;<br />}<br />}<br /><br />return location;<br />}/*end linearSearch function */<br /><br />void addDelete (char name[SIZE][LENGTH], int crn[SIZE][4], int location)<br />{<br />int i, temp;<br />//char invoice;<br />char prefix[7];<br />int hours;<br />char selection;<br /><br />printf ("nHere are the courses [%s] is taking: ", name[location]);<br />printf ("nCRNtPREFIXtCR HOURS");<br />for ( i= 0; i<4; i++)<br />{<br />if (crn[location]</i><i> != 0)<br />{<br />getInfo ( crn[location]</i><i>, prefix , &hours);<br />printf ("n%dtt%stt%d", crn[location]</i><i>, prefix, hours);<br />}<br />else<br />break;<br />}<br />printf ("nnChoose from:");<br />printf ("nA - Add a new course for [%s]", name[location]);<br />printf ("nD - Delete a course from [%s]'s schedule", name[location]);<br />printf ("nC - Cancel operation");<br />printf ("nEnter your selection here: ");<br />fflush (stdin);<br />scanf ("%c", &selection);<br /><br />if (selection == 'A' || selection == 'a')<br />{<br /><br />for ( i= 0; i<4; i++)<br />{<br />if (crn[location]</i><i> == 0)<br />{<br />printf ("nEnter course number to add: ");<br />scanf ("%d", &crn[location]</i><i>);<br />getInfo (crn[location]</i><i>, prefix, &hours);<br />printf ("n[%d %s] successfully added!n", crn[location]</i><i>, prefix);<br />break;<br />}<br /><br />if (i==3 && crn[location]</i><i> != 0)<br />{<br />printf ("nYou cannot have more than 4 courses.");<br />return;<br />}<br />}<br /><br /><br />}<br /><br />else if (selection == 'D' || selection == 'd')<br />{<br />printf ("nEnter course number to delete: ");<br />scanf ("%d", &temp);<br />for ( i= 0; i<4; i++)<br />{<br />if (temp == crn[location]</i><i>)<br />{<br />getInfo (crn[location]</i><i>, prefix, &hours);<br />printf ("n[%d %s] successfully deleted!n", crn[location]</i><i>, prefix);<br />crn[location]</i><i>=0;<br />while ( i<4)<br />{<br />crn[location]</i><i> = crn[location][i+1];<br />i++;<br />}<br />break;<br />}<br />}<br />}<br />else if (selection == 'C' || selection == 'c')<br />{<br />return;<br />}<br />else<br />{<br />printf ("nInvalid selection.");<br />}<br /><br /><br /><br /><br />}<br /><br />void getInfo (int crn, char prefix[7], int *hours)<br />{<br />switch (crn)<br />{<br />case 4587:<br />{<br />strcpy (prefix, "MAT 236");<br />*hours = 4;<br />break;<br />}<br />case 4599:<br />{<br />strcpy (prefix, "COP 220");<br />*hours = 3;<br />break;<br />}<br />case 8997:<br />{<br />strcpy (prefix, "GOL 124");<br />*hours = 1;<br />break;<br />}<br />case 9696:<br />{<br />strcpy (prefix, "COP 100");<br />*hours = 3;<br />break;<br />}<br />}<br />}<br /><br />void printInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location)<br />{<br /><br />double total = 0;<br />int hours;<br />char prefix[7];<br />int i;<br />double subTotal;<br /><br />total = ID_FEE;<br /><br />printf ("nnnnttVALENCE COMMUNITY COLLEGE");<br />printf ("nttORLANDO FL 10101");<br />printf ("ntt*************************");<br />printf ("nnttFee Invoice Prepared for Student:");<br />printf ("ntt%d - [%s]", id[location], name[location]);<br />printf ("nntt1 Credit Hour = $%.2lf", COSTPERCREDITHOUR);<br />printf ("nnttCRNtPREFIXt HOURS");<br />for ( i= 0; i<4; i++)<br />{<br />if (crn[location]</i><i> != 0)<br />{<br />getInfo ( crn[location]</i><i>, prefix , &hours);<br />subTotal = hours * COSTPERCREDITHOUR;<br />printf ("ntt%dt%stt%dt$%.2lf", crn[location]</i><i>, prefix , hours, subTotal);<br /><br />total = total + subTotal;<br />}else if (crn[location]</i><i> == 0)<br />break;<br />}<br />printf ("nntttHealth & ID Feest$ %.2lf", ID_FEE);<br />printf ("nntt</i><hr class="bbcode_rule" />");<br />printf ("ntttTotal Paymentstt$%.2lfnn", total);<br />}<br /><br />void sortInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location)<br />{<br />double total = 0;<br />int hours;<br />char prefix[7];<br />int i;<br />double subTotal;<br />int j, temp ;<br /><br /><br />for ( j= 0; j<size ; j++)<br></size>{<br />for ( i=0; i<=SIZE-2 ; i++)<br />{<br />if ( crn[location]<i> > crn[location][i+1] && crn[location][i+1] != 0 )<br />{<br />temp = crn[location]</i><i>;<br />crn[location]</i><i> = crn[location][i+1];<br />crn[location][i+1] = temp;<br />}<br />}<br />}<br /><br /><br />total = ID_FEE;<br /><br />printf ("nnnnttVALENCE COMMUNITY COLLEGE");<br />printf ("nttORLANDO FL 10101");<br />printf ("ntt*************************");<br />printf ("nnttFee Invoice Prepared for Student:");<br />printf ("ntt%d - [%s]", id[location], name[location]);<br />printf ("nntt1 Credit Hour = $%.2lf", COSTPERCREDITHOUR);<br />printf ("nnttCRNtPREFIXt HOURS");<br />for ( i= 0; i<4; i++)<br />{<br />if (crn[location]</i><i> != 0)<br />{<br />getInfo ( crn[location]</i><i>, prefix , &hours);<br />subTotal = hours * COSTPERCREDITHOUR;<br />printf ("ntt%dt%stt%dt$%.2lf", crn[location]</i><i>, prefix , hours, subTotal);<br /><br />total = total + subTotal;<br />}else if (crn[location]</i><i> == 0)<br />break;<br />}<br />printf ("nntttHealth & ID Feest$ %.2lf", ID_FEE);<br />printf ("nntt</i><hr class="bbcode_rule" />");<br />printf ("ntttTotal Paymentstt$%.2lfnn", total);<br />}<br /> - July 27, 2009 at 5:34 pm #3596GWILouisaxwzklaParticipant
You really just need to group all your data together ( structs are handy for this purpose ). So if you have a function prototype like:
123<br />void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);<br />change it to:
123<br />void addStudent ( STUDENT newStudent );<br />when you need to access a member of the struct , just use the dot operator and use the data as usual:
12345<br />..........<br />printf ( newStudent.name ); //output the student name<br />printf ( "%i" , newStudent.id ); //output the student id<br />not too much to the conversion …….
- July 27, 2009 at 6:17 pm #3597MaricruDelucaParticipant
Thank you!
- July 27, 2009 at 7:00 pm #3598GWILouisaxwzklaParticipant
A little correction. It seems you are passing arrays of names , id’s and crn into the function:
123<br />void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);<br />so you would change your prototype to :
1234<br />const int numberOfStudents = 20; //whatever<br />void addStudent ( STUDENT newStudent [ numberOfStudents ] );<br />and access the data members like this:
1234567<br />..........<br />int i = currentStudent;<br />printf ( newStudent [ currentStudent ].name ); //output the student name<br />printf ( "%i" , newStudent [ currentStudent ] .id ); //output the student id<br /><br />my bad ………
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.