944,181 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2750
  • C RSS
Dec 3rd, 2007
0

help with program using structures and printing to screen

Expand Post »
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.  
Similar Threads
Reputation Points: 31
Solved Threads: 0
Newbie Poster
controlsi is offline Offline
7 posts
since Nov 2007
Dec 3rd, 2007
0

Re: help with program using structures and printing to screen

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

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: having problems with structure.
Next Thread in C Forum Timeline: The Clear





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


Follow us on Twitter


© 2011 DaniWeb® LLC