help with program using structures and printing to screen

Reply

Join Date: Nov 2007
Posts: 7
Reputation: controlsi is an unknown quantity at this point 
Solved Threads: 0
controlsi controlsi is offline Offline
Newbie Poster

help with program using structures and printing to screen

 
0
  #1
Dec 3rd, 2007
This code is suppose to read from an inputed file and then fill and array and sort the student records with in that file. Im having alot of trouble debugging myself and this program is due with in two days.
the input will look like
lastname, firstname grade ssn birthday date of first attendence
my main problem is mostly the coding from 46 to 78 i know i have mistakes but im a pretty new to c programming.
thanks
david


  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define STR_SIZE 25
  5. #define ARR_SIZE 10
  6. #define MAX 25
  7.  
  8. typedef struct // structure that will be used in the program
  9. {
  10. char month [MAX];
  11. int day;
  12. int year;
  13. }DATE;
  14.  
  15. typedef struct
  16. {
  17. char social[9];
  18. }SSN;
  19.  
  20. typedef struct
  21. {
  22. int grade;
  23. }GRADE;
  24.  
  25. typedef struct
  26. {
  27. char firstname[MAX];
  28. char lastname[MAX];
  29. DATE birthday;
  30. SSN social[9];
  31. GRADE stugrade;
  32. }INFO;
  33.  
  34.  
  35. int fillArray(FILE *inf, arr[STR_SIZE]); // to fill the array from a the file
  36. void printArray(arr[], int numEls);// error messages are showing that there is problem before 'arr'
  37.  
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41. int numEls;
  42.  
  43. arr[][STR_SIZE]; //is this legal for me to use?
  44.  
  45. FILE *inf;
  46. enum months {January = 1, February, March, April, May, June, July,
  47. August, September, November, December};
  48. enum months DateMonth;
  49. SSN snum;
  50. inf = fopen(argv[1], "r");
  51. numEls = fillArray(inf, arr);
  52. printArray(arr, numEls);
  53. fclose(inf);
  54.  
  55. return 0;
  56. }
  57.  
  58. int fillArray(FILE *inf, arr[]);
  59. {
  60. int i;
  61.  
  62. for (i = 0; i < STR_SIZE; i++)
  63. scanf("%s", arr[i]);
  64. }
  65. void printArray(arr[], int numEls);
  66. {
  67. int i ;
  68. for (i = 0; i < STR_SIZE; i++)
  69. scanf("%s", arr[i]); // is this the right way to fill an array or am i really wrong?
  70. }
  71. void printArray(arr[], int numEls);
  72. {
  73. int i ;
  74. for (i = 0; i < STR_SIZE; i++)
  75. printf("Name: %s, %s Grade: %d SSN: %s DOB: %s %d, %d 1st Attend: %s %d, %d, ); // i know i still have to add the addresses for this to work
  76. }
  77.  
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: help with program using structures and printing to screen

 
0
  #2
Dec 3rd, 2007
Starting somewhere for no particular reason:

  1. arr[][STR_SIZE]; //is this legal for me to use?
It would be legal if you would have initialized arr, however since you didn't the compiler doesn't know how much space must set apart for arr.

arr[][STR_SIZE] = { "One", "Two", "Three" }; /* This would be OK */

  1. typedef struct
  2. {
  3. char social[9];
  4. }SSN;
  5.  
  6. typedef struct
  7. {
  8. int grade;
  9. }GRADE;

Why to create a whole new data type structure for just one element inside?


  1. int fillArray(FILE *inf, arr[]);

Not quite sure why you need to pass a pointer to a file handle. You are not doing anything with it inside the function.
arr is an array of arrays. You are just passing it as an array or string.

The easiest way is to prototype the multidimensional array the same way you declared
if arr[][STR_SIZE] then type_data function_name( arr[][STR_SIZE] );
if arr[MAX][MAX] then type_data function_name( arr[][MAX] );

  1. scanf("%s", arr[i]); // is this the right way to fill an array or am i really wrong?
If you would have pass arr the proper way it would have a chance of being correct, other than scanf is not a good way of obtaining string data from user.
Search for fgets()
Last edited by Aia; Dec 3rd, 2007 at 9:10 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC