Ok here is the gist of my problem, I am trying to use arrays, linked lists, and functions all together and somehow make it all work.
The idea is to have a number of records read in from a file one line at a time. However when I find a record that meets certain criteria, I branch off and create a dynamic linked list attached to my array structure. The problem I am having is not when I populate my array and lists it is when I read back the values in the linked-lists. It appears that I can only do this once.
I have an idea that my problem is because I never point the list back to the "top", but I cannot figure out the best way to do so.
I am starting to really get frustrated and confused by the linked-lists. And was hoping that some "pointers" yes poor pun intended... could be thrown my way.
thanks everyone,
shane
Code:
//Type Definitions
typedef struct dep_db
{
char dpdnt_data[DPDNT_LENGTH + 1];
struct dep_db* next;
} DEP_DB_T;
typedef struct tax_db
{
char name[NAME_LENGTH + 1];
char ssn[SSN_LENGTH + 1];
double dpdnts;
char mar_stat;
double tax_inc;
DEP_DB_T* dep_list;
} TAX_DB_T;
pop_array2 (data_io, tax_record, &array_cnt);
disp_taxpyr_dep (tax_record, array_cnt);
void pop_array2 (FILE *readptr, TAX_DB_T *tax_record, int *array_cnt)
{
// Variable Declarations
int x=0; // Counter for array of structures
int y=0; // Counter for linked list
int Len;
DEP_DB_T *current;
// Start Function Code
for (x=0; !feof(readptr) && x < MAX_ARRAY_SIZE; x++)
{
fscanf (readptr, "\n%25c\n%11c%1lf\n%c%lf\n", tax_record[x].name, tax_record[x].ssn, &tax_record[x].dpdnts, &tax_record[x].mar_stat, &tax_record[x].tax_inc);
tax_record[x].mar_stat = toupper(tax_record[x].mar_stat);
if (tax_record[x].dpdnts != 0)
{
y = tax_record[x].dpdnts;
current = tax_record[x].dep_list = (DEP_DB_T *) malloc(sizeof(DEP_DB_T));
fgets (current -> dpdnt_data, DPDNT_LENGTH + 2, readptr);
Len = strlen (current -> dpdnt_data);
current -> dpdnt_data [Len-1] = '\0';
y--;
while (y != 0)
{
if ((current->next = (DEP_DB_T *) malloc( sizeof ( DEP_DB_T ) ) ) == NULL )
{
printf ("Youre out of memory");
}
current = current -> next;
fgets (current -> dpdnt_data, DPDNT_LENGTH + 2, readptr);
Len = strlen (current -> dpdnt_data);
current -> dpdnt_data [Len-1] = '\0';
y--;
}
current -> next = NULL;
}
}
*array_cnt = x;
printf ("\n [%d] Records Read Into Taxpayer Information DB", *array_cnt);
}
void disp_taxpyr_dep (TAX_DB_T *tax_record, int array_cnt)
{
// Variable Declarations
int x; // FOR loop counter
// Start Function Code
printf ("\n\n ** Taxpayer List **");
printf ("\n\n Taxpayer SSN Number of Marital Taxable");
printf ("\n Dependents Status Income ");
printf ("\n ------------------------- ----------- ---------- ------- -----------\n");
for (x=0; x < array_cnt; x++)
{
printf ("\n %s %s %1.0lf ", tax_record[x].name, tax_record[x].ssn, tax_record[x].dpdnts);
if (tax_record[x].mar_stat == 'M')
printf ("Married ");
else
printf ("Single ");
printf ("$ %9.2lf", tax_record[x].tax_inc);
if (tax_record[x].dpdnts != 0)
{
while (tax_record[x].dep_list != NULL)
{
printf ("\n%s", tax_record[x].dep_list -> dpdnt_data);
tax_record[x].dep_list = tax_record[x].dep_list -> next;
}
}
}
printf ("\n\n\t\tPress <ENTER> To Return To Taxpayer Main Menu");
getchar();
getchar();
}
Any more code needed just ask.