| | |
help with program using structures and printing to screen
![]() |
•
•
Join Date: Nov 2007
Posts: 7
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #define STR_SIZE 25 #define ARR_SIZE 10 #define MAX 25 typedef struct // structure that will be used in the program { char month [MAX]; int day; int year; }DATE; typedef struct { char social[9]; }SSN; typedef struct { int grade; }GRADE; typedef struct { char firstname[MAX]; char lastname[MAX]; DATE birthday; SSN social[9]; GRADE stugrade; }INFO; int fillArray(FILE *inf, arr[STR_SIZE]); // to fill the array from a the file void printArray(arr[], int numEls);// error messages are showing that there is problem before 'arr' int main(int argc, char *argv[]) { int numEls; arr[][STR_SIZE]; //is this legal for me to use? FILE *inf; enum months {January = 1, February, March, April, May, June, July, August, September, November, December}; enum months DateMonth; SSN snum; inf = fopen(argv[1], "r"); numEls = fillArray(inf, arr); printArray(arr, numEls); fclose(inf); return 0; } int fillArray(FILE *inf, arr[]); { int i; for (i = 0; i < STR_SIZE; i++) scanf("%s", arr[i]); } void printArray(arr[], int numEls); { int i ; for (i = 0; i < STR_SIZE; i++) scanf("%s", arr[i]); // is this the right way to fill an array or am i really wrong? } void printArray(arr[], int numEls); { int i ; for (i = 0; i < STR_SIZE; i++) 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 }
Starting somewhere for no particular reason:
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 */
Why to create a whole new data type structure for just one element inside?
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] );
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()
C Syntax (Toggle Plain Text)
arr[][STR_SIZE]; //is this legal for me to use?
arr[][STR_SIZE] = { "One", "Two", "Three" }; /* This would be OK */
C Syntax (Toggle Plain Text)
typedef struct { char social[9]; }SSN; typedef struct { int grade; }GRADE;
Why to create a whole new data type structure for just one element inside?
C Syntax (Toggle Plain Text)
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] );
C Syntax (Toggle Plain Text)
scanf("%s", arr[i]); // is this the right way to fill an array or am i really wrong?
Search for fgets()
Last edited by Aia; Dec 3rd, 2007 at 9:10 pm.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: having problems with structure.
- Next Thread: The Clear
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






