943,570 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 499
  • C RSS
Jul 26th, 2009
0

How to redo the same code using structs

Expand Post »
This is the code that i have, my project is to do this exact code, but with structs. I'm obviously very shaky on the structs concepts, however, i really need to get this assignment done.

Any help in this matter would be greatly appreciated!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define SIZE 5
  5. #define LENGTH 41
  6. #define COSTPERCREDITHOUR 120.25
  7. #define ID_FEE 35.00
  8.  
  9. //need to do same project using only structs
  10. typedef struct{
  11. int id;
  12. int crn[4];
  13. int name[40];
  14. }STUDENT;
  15.  
  16. //void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);
  17. void addStudent(STUDENT []);
  18. void menu (int *option);
  19. int linearSearch (int id[SIZE], int key);
  20. void addDelete (char name[SIZE][LENGTH], int crn[SIZE][4], int location);
  21. void getInfo (int crn, char prefix[7], int *hours);
  22. void printInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location);
  23. void sortInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location);
  24. //------------------------------------------------------------------------------
  25. int main ()
  26. {
  27. char name[SIZE][LENGTH] = {""};
  28. int id[SIZE] = {0};
  29. int crn[SIZE][4] = {{0}};
  30. char invoice;
  31.  
  32. int option = 0, loop = 0, key = 0, location = 0;
  33.  
  34. while (loop == 0)
  35. {
  36. menu (&option);
  37.  
  38. switch (option)
  39. {
  40. case 1:
  41. {
  42. addStudent (name, id, crn);
  43. break;
  44. }
  45. case 2:
  46. {
  47. printf ("Enter the students ID: ");
  48. scanf ("%d", &key);
  49. location = linearSearch (id, key);
  50. addDelete (name, crn, location);
  51. printf ("\nWould you like to display a new invoice? (Y/N): ");
  52. fflush (stdin);
  53. scanf ("%c", &invoice);
  54. if (invoice == 'y' || invoice == 'Y')
  55. {
  56. printInvoice (id, name, crn, location);
  57. }
  58. break;
  59. }
  60. case 3:
  61. {
  62. printf ("Enter the students ID: ");
  63. scanf ("%d", &key);
  64. location = linearSearch (id, key);
  65. if (location == -1)
  66. printf ("\nNo student found!\n");
  67. else
  68. printf ("\nStudent found @ index location %d.\n", location);
  69. break;
  70. }
  71. case 4:
  72. {
  73. printf ("Enter the students ID: ");
  74. scanf ("%d", &key);
  75. location = linearSearch (id, key);
  76. printInvoice (id, name, crn, location);
  77. break;
  78. }
  79. case 5:
  80. {
  81. printf ("Enter the students ID: ");
  82. scanf ("%d", &key);
  83. location = linearSearch (id, key);
  84. sortInvoice (id, name, crn, location);
  85. break;
  86. }
  87. case 0:
  88. {
  89. loop=1;
  90. break;
  91. }
  92. default:
  93. {
  94. printf ("Invalid option, try again.\n");
  95. break;
  96. }
  97.  
  98. }
  99. }
  100.  
  101. printf ("Goodbye!\n\n\n");
  102.  
  103. system ("pause");
  104. return 0;
  105. }/*end main function*/
  106.  
  107. void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4])
  108. {
  109. char temp[LENGTH];
  110. int i=0 , n , j;
  111. int finder;
  112.  
  113.  
  114. printf("Enter the ID: ");
  115. scanf("%d", &finder);
  116. while ( i < 4 && finder != id[i])
  117. i++;
  118. if(finder == id[i])
  119. {
  120. printf("\nThe ID %d is already assigned to a student", finder);
  121. return;
  122. }else if (finder != id[i])
  123. {
  124.  
  125.  
  126. printf ("Enter the name of the student: ");
  127. //fflush (stdin);
  128. gets (temp);
  129. gets (temp);
  130.  
  131. for ( i=0; i<SIZE; i++)
  132. {
  133. if ( strcmp (name[i] , "") == 0 )
  134. {
  135. id[i] = finder;
  136. strcpy ( name[i], temp);
  137. printf ("Enter how many CRNs %s is taking: ", temp);
  138. scanf ("%d", &n);
  139. if (n > 4)
  140. {
  141. printf ("\nYou cannot take more than 4 classes.\n");
  142. strcpy (name, "");
  143. id[i] = 0;
  144. return;
  145. }
  146.  
  147. else if (n < 0)
  148. {
  149. printf ("\nYou must take at least one class.\n");
  150. strcpy (name, "");
  151. id[i] = 0;
  152. return;
  153. }
  154.  
  155. else
  156. {
  157. printf ("Enter the %d CRNs: ", n);
  158. for (j=0; j<n ; j++)
  159. {
  160. scanf ("%d", &crn[i][j]);
  161. }
  162. printf ("\nStudent added!\n");
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. }/*end addStudent function*/
  169.  
  170.  
  171. void menu (int *option)
  172. {
  173. printf ("\tWelcome!\n");
  174. printf ("Choose from the following options:\n\n");
  175. printf("=============================================\n");
  176. printf ("1 - Add a new student\n");
  177. printf ("2 - Add/delete a course\n");
  178. printf ("3 - Search for a student\n");
  179. printf ("4 - Print fee invoice\n");
  180. printf ("5 - Print fee invoice sorted by CRN\n");
  181. printf ("0 - Exit program\n");
  182. printf ("\nEnter your selection: ");
  183. scanf ("%d", option);
  184.  
  185. }/*end menu option*/
  186.  
  187.  
  188. int linearSearch (int id[SIZE], int key)
  189. {
  190. int i;
  191. int location = -1;
  192.  
  193. for ( i= 0; i<SIZE; i++)
  194. {
  195. if (id[i] == key )
  196. {
  197. location = i;
  198. break;
  199. }
  200. }
  201.  
  202. return location;
  203. }/*end linearSearch function */
  204.  
  205. void addDelete (char name[SIZE][LENGTH], int crn[SIZE][4], int location)
  206. {
  207. int i, temp;
  208. //char invoice;
  209. char prefix[7];
  210. int hours;
  211. char selection;
  212.  
  213. printf ("\nHere are the courses [%s] is taking: ", name[location]);
  214. printf ("\nCRN\tPREFIX\tCR HOURS");
  215. for ( i= 0; i<4; i++)
  216. {
  217. if (crn[location][i] != 0)
  218. {
  219. getInfo ( crn[location][i], prefix , &hours);
  220. printf ("\n%d\t\t%s\t\t%d", crn[location][i], prefix, hours);
  221. }
  222. else
  223. break;
  224. }
  225. printf ("\n\nChoose from:");
  226. printf ("\nA - Add a new course for [%s]", name[location]);
  227. printf ("\nD - Delete a course from [%s]'s schedule", name[location]);
  228. printf ("\nC - Cancel operation");
  229. printf ("\nEnter your selection here: ");
  230. fflush (stdin);
  231. scanf ("%c", &selection);
  232.  
  233. if (selection == 'A' || selection == 'a')
  234. {
  235.  
  236. for ( i= 0; i<4; i++)
  237. {
  238. if (crn[location][i] == 0)
  239. {
  240. printf ("\nEnter course number to add: ");
  241. scanf ("%d", &crn[location][i]);
  242. getInfo (crn[location][i], prefix, &hours);
  243. printf ("\n[%d %s] successfully added!\n", crn[location][i], prefix);
  244. break;
  245. }
  246.  
  247. if (i==3 && crn[location][i] != 0)
  248. {
  249. printf ("\nYou cannot have more than 4 courses.");
  250. return;
  251. }
  252. }
  253.  
  254.  
  255. }
  256.  
  257. else if (selection == 'D' || selection == 'd')
  258. {
  259. printf ("\nEnter course number to delete: ");
  260. scanf ("%d", &temp);
  261. for ( i= 0; i<4; i++)
  262. {
  263. if (temp == crn[location][i])
  264. {
  265. getInfo (crn[location][i], prefix, &hours);
  266. printf ("\n[%d %s] successfully deleted!\n", crn[location][i], prefix);
  267. crn[location][i]=0;
  268. while ( i<4)
  269. {
  270. crn[location][i] = crn[location][i+1];
  271. i++;
  272. }
  273. break;
  274. }
  275. }
  276. }
  277. else if (selection == 'C' || selection == 'c')
  278. {
  279. return;
  280. }
  281. else
  282. {
  283. printf ("\nInvalid selection.");
  284. }
  285.  
  286.  
  287.  
  288.  
  289. }
  290.  
  291. void getInfo (int crn, char prefix[7], int *hours)
  292. {
  293. switch (crn)
  294. {
  295. case 4587:
  296. {
  297. strcpy (prefix, "MAT 236");
  298. *hours = 4;
  299. break;
  300. }
  301. case 4599:
  302. {
  303. strcpy (prefix, "COP 220");
  304. *hours = 3;
  305. break;
  306. }
  307. case 8997:
  308. {
  309. strcpy (prefix, "GOL 124");
  310. *hours = 1;
  311. break;
  312. }
  313. case 9696:
  314. {
  315. strcpy (prefix, "COP 100");
  316. *hours = 3;
  317. break;
  318. }
  319. }
  320. }
  321.  
  322. void printInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location)
  323. {
  324.  
  325. double total = 0;
  326. int hours;
  327. char prefix[7];
  328. int i;
  329. double subTotal;
  330.  
  331. total = ID_FEE;
  332.  
  333. printf ("\n\n\n\n\t\tVALENCE COMMUNITY COLLEGE");
  334. printf ("\n\t\tORLANDO FL 10101");
  335. printf ("\n\t\t*************************");
  336. printf ("\n\n\t\tFee Invoice Prepared for Student:");
  337. printf ("\n\t\t%d - [%s]", id[location], name[location]);
  338. printf ("\n\n\t\t1 Credit Hour = $%.2lf", COSTPERCREDITHOUR);
  339. printf ("\n\n\t\tCRN\tPREFIX\t HOURS");
  340. for ( i= 0; i<4; i++)
  341. {
  342. if (crn[location][i] != 0)
  343. {
  344. getInfo ( crn[location][i], prefix , &hours);
  345. subTotal = hours * COSTPERCREDITHOUR;
  346. printf ("\n\t\t%d\t%s\t\t%d\t$%.2lf", crn[location][i], prefix , hours, subTotal);
  347.  
  348. total = total + subTotal;
  349. }else if (crn[location][i] == 0)
  350. break;
  351. }
  352. printf ("\n\n\t\t\tHealth & ID Fees\t$ %.2lf", ID_FEE);
  353. printf ("\n\n\t\t---------------------------------------");
  354. printf ("\n\t\t\tTotal Payments\t\t$%.2lf\n\n", total);
  355. }
  356.  
  357. void sortInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location)
  358. {
  359. double total = 0;
  360. int hours;
  361. char prefix[7];
  362. int i;
  363. double subTotal;
  364. int j, temp ;
  365.  
  366.  
  367. for ( j= 0; j<SIZE ; j++)
  368. {
  369. for ( i=0; i<=SIZE-2 ; i++)
  370. {
  371. if ( crn[location][i] > crn[location][i+1] && crn[location][i+1] != 0 )
  372. {
  373. temp = crn[location][i];
  374. crn[location][i] = crn[location][i+1];
  375. crn[location][i+1] = temp;
  376. }
  377. }
  378. }
  379.  
  380.  
  381. total = ID_FEE;
  382.  
  383. printf ("\n\n\n\n\t\tVALENCE COMMUNITY COLLEGE");
  384. printf ("\n\t\tORLANDO FL 10101");
  385. printf ("\n\t\t*************************");
  386. printf ("\n\n\t\tFee Invoice Prepared for Student:");
  387. printf ("\n\t\t%d - [%s]", id[location], name[location]);
  388. printf ("\n\n\t\t1 Credit Hour = $%.2lf", COSTPERCREDITHOUR);
  389. printf ("\n\n\t\tCRN\tPREFIX\t HOURS");
  390. for ( i= 0; i<4; i++)
  391. {
  392. if (crn[location][i] != 0)
  393. {
  394. getInfo ( crn[location][i], prefix , &hours);
  395. subTotal = hours * COSTPERCREDITHOUR;
  396. printf ("\n\t\t%d\t%s\t\t%d\t$%.2lf", crn[location][i], prefix , hours, subTotal);
  397.  
  398. total = total + subTotal;
  399. }else if (crn[location][i] == 0)
  400. break;
  401. }
  402. printf ("\n\n\t\t\tHealth & ID Fees\t$ %.2lf", ID_FEE);
  403. printf ("\n\n\t\t---------------------------------------");
  404. printf ("\n\t\t\tTotal Payments\t\t$%.2lf\n\n", total);
  405. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jahmassive is offline Offline
2 posts
since Apr 2009
Jul 26th, 2009
0

Re: How to redo the same code using structs

jahmassive> This is the code that i have, my project is to do this exact code, but with structs

Instead of dumping some source code in a post, and expecting others to find out what it is intended to do, you could explain what the code is supposed to do, and where are you having problems in the implementations of structures. Just a thought.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jul 26th, 2009
0

Re: How to redo the same code using structs

ok, maybe that reply was deserved so i'll take that one. The purpose of the assignment is to create a fee invoice for students at a college. They can add classes, delete them, you can search for a student, print a fee invoice, and print a fee invoice sorted by a crn.

This program works the way it is, the only thing i am trying to do is the exact same thing with structs.

Reputation Points: 10
Solved Threads: 0
Newbie Poster
jahmassive is offline Offline
2 posts
since Apr 2009
Jul 26th, 2009
1

Re: How to redo the same code using structs

jahmassive> This program works the way it is
Which doesn't mean it will always do.

gets() is a deal breaker. If you forget that gets() exist, you'll be doing yourself a favor.

fflush (stdin) is another deal breaker. It was not intended to be used with stdin but rather stdout.

system ("pause"); is foreigner to many causes. It works with OS that knows what to do with it.

scanf() is a wolf in sheep's clothing. Using it to obtain input from user can destroy your program.

Here's a simplistic example of the use of structures. Because I am not using pointers, any structure passed as parameter is a copy of the original.

  1. /* structures.c
  2.  * testing how structures works in C
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. /* defining a structure */
  8. struct student {
  9. char name[40]; /* This is an array of chars and not ints */
  10. int id;
  11. int whatever;
  12. };
  13.  
  14. void disply_student(struct student s);
  15. struct student add_student(struct student s);
  16.  
  17. int main(void)
  18. {
  19. /* declare a student structure */
  20. struct student pupil;
  21.  
  22. /* calling add function */
  23. pupil = add_student(pupil);
  24.  
  25. /* display resut */
  26. disply_student(pupil);
  27.  
  28. return 0;
  29. }
  30.  
  31. /* define a function to use structure student */
  32. struct student add_student(struct student s)
  33. {
  34. /* adding name*/
  35. strcpy(s.name, "John");
  36.  
  37. /* adding id */
  38. s.id = 3;
  39.  
  40. /* adding whatever */
  41. s.whatever = 15;
  42.  
  43. /* returning s copies data to original student */
  44. return s;
  45. }
  46.  
  47. void disply_student(struct student s)
  48. {
  49. printf("Name: %s\n", s.name);
  50. printf("ID: %d\n", s.id);
  51. printf("Whatever: %d\n", s.whatever);
  52. }
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jul 27th, 2009
0

Re: How to redo the same code using structs

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: pattern printin
Next Thread in C Forum Timeline: Ideas for a c project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC