-
Linked List problem
How do I write a C program that will use a linked list of structures to
represent the customers of a bank?
Each structure has to be designed
to hold information regarding a persons
how would i start? ima newbie to this and i dont quite get it
(1) Last name
(2) First name
(3) Middle initial
(4) Social Security number
(5) Bank balance
-
A quick look in the tutorials of this site turned up This
-
My post here should clue you in.
Quzah.
-
Looks like homework to me.....
but anyway first define the structure:
[CODE]
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
} CUSTOMER;
then create a pointer to this:
CUSTOMER *bankrecords;
assign memory:
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
then do what ya want with it ie to add a name do:
strcpy(bankrecords->fname,"FRED");
to add a new customer go to the end of the list, ie
tempptr = bankrecords;
while(tempptr->next!=NULL)
tempptr=tempptr->next;
then assign more memory
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
and repeat the adding, I'm sure you can work out the rest otherwise its not really homework where ya learning anything at all.
Hope this helps
-
thank you all very much, you have been of great help, it is a homework assignment but becuase im a biology major and not a computer science major its difficult for me but im working on it
-
i have a problem i need to be able to read data from a file and im not sure how to do it. This is what i have so far.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
};
int main(void)
{
FILE *infile;
infile= fopen("data.dat","r");
fclose(data.dat)
return 0;
}
-
fgets reads a line.
fscanf works like scanf.
fgetc reads a character.
That should get you started. When all else fails, consult the man pages.
Quzah.
-
those will not work when im trying to read the data into a structure. I have a data file that has info like this:
Moon Warren H 637801463 19473.08
So it has to be able to read the first name into bankrecords.fname and last name into bankrecords.lname and so on.
-
fscanf will work perfectly, just do:
Code:
fscanf(infile,"%s %s %c %d %f",(*bankrecords)->fname,(*bankrecords)->lname,bankrecords->initial,bankrecords->account,bankrecords->balance);
think the pointers and *'s are right not 100% but pretty sure about the first two, and slightly to lazy to double check :)
p.s. make sure you assign the memory before you do the fscanf or you'll have problems.
-
When i use
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
it gives me errors
-
you need to assign your structure a name, if you have a look at the code example I gave it will work:
Code:
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
} CUSTOMER;
the last line : } CUSTOMER;
allow you to treat this as a standard data type and decalre variables like:
CUSTOMER john;
and then you have a variable called john.
alternatevly you could use:
bankrecords = (struct customer *) malloc(sizeof(struct customer));
I think, but the first way will work and make it heaps easier to assign new variables, etc.
-
I have this and it says my variables are not identified.
[code]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
};
int main()
{
FILE *infile;
struct customer *bankrecords;
int count=1;
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
if((infile = fopen("data.dat","r")) == NULL) {
printf("Data file could not be opened.\n");
exit(1);
}
infile=fopen("data.dat","r");
printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
printf("-----------------------------------------------------\n");
while(!feof(infile)) {
//fread(&bankrecords,sizeof(struct customer),1,infile);
fscanf(infile,"%s %s %c %d %f",bankrecords.fname,bankrecords.lname,bankrecord s.initial,bankrecords.socialnum,bankrecords.balanc e);
printf("\n %s",bankrecords.fname);
}
fclose(infile);
return 0;
}
[code\]
-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
int count=1;
bankrecords = (struct customer *) malloc(sizeof(struct customer));
if((infile = fopen("data.dat","r")) == NULL) {
printf("Data file could not be opened.\n");
exit(1);
}
infile=fopen("data.dat","r");
printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
printf("-----------------------------------------------------\n");
while(!feof(infile)) {
//fread(&bankrecords,sizeof(struct customer),1,infile);
fscanf(infile,"%s %s %c %d %f",bankrecords.fname,bankrecords.lname,bankrecord s.initial,bankrecords.socialnum,bankrecords.balanc e);
printf("\n %s",bankrecords.fname);
}
fclose(infile);
return 0;
}
ok im gettin error messages that say.............
--------------------Configuration: lab5 - Win32 Debug--------------------
Compiling...
lab5.cpp
C:\Documents and Settings\All Users\Documents\lab5.cpp(22) : error C2065: 'bankrecords' : undeclared identifier
C:\Documents and Settings\All Users\Documents\lab5.cpp(22) : error C2440: '=' : cannot convert from 'struct customer *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.fname' must have class/struct/union type
C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.lname' must have class/struct/union type
C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.initial' must have class/struct/union type
C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.socialnum' must have class/struct/union type
C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.balance' must have class/struct/union type
C:\Documents and Settings\All Users\Documents\lab5.cpp(36) : error C2228: left of '.fname' must have class/struct/union type
Error executing cl.exe.
lab5.obj - 8 error(s), 0 warning(s)
-
don't use dot notation to access that structure's data fields. Use the -> operator.
struct customer *john;
john -> balance = 2.54;
And try,..
CUSTOMER bankrecords = (struct customer *) malloc(sizeof(struct customer));
-
ok where are you gettin john->balance from? and i still get errors with my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
int count=1;
CUSTOMER bankrecords = (struct customer *) malloc(sizeof(struct customer));
if((infile = fopen("data.dat","r")) == NULL) {
printf("Data file could not be opened.\n");
exit(1);
}
infile=fopen("data.dat","r");
printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
printf("-----------------------------------------------------\n");
while(!feof(infile)) {
//fread(&bankrecords,sizeof(struct customer),1,infile);
fscanf(infile,"%s %s %c %d %f",CUSTOMER bankrecords->fname,CUSTOMER bankrecords->lname,CUSTOMER bankrecords->initial,CUSTOMER bankrecords->socialnum,CUSTOMER bankrecords->balance);
printf("\n %s",bankrecords->fname);
}
fclose(infile);
return 0;
}
--------------------Configuration: lab5 - Win32 Debug--------------------
Compiling...
lab5.cpp
C:\Documents and Settings\All Users\Documents\lab5.cpp(22) : error C2440: 'initializing' : cannot convert from 'struct customer *' to 'struct customer'
No constructor could take the source type, or constructor overload resolution was ambiguous
C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2275: 'CUSTOMER' : illegal use of this type as an expression
C:\Documents and Settings\All Users\Documents\lab5.cpp(13) : see declaration of 'CUSTOMER'
C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2146: syntax error : missing ')' before identifier 'bankrecords'
C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2059: syntax error : ')'
C:\Documents and Settings\All Users\Documents\lab5.cpp(36) : error C2819: type 'customer' does not have an overloaded member 'operator ->'
C:\Documents and Settings\All Users\Documents\lab5.cpp(6) : see declaration of 'customer'
C:\Documents and Settings\All Users\Documents\lab5.cpp(36) : error C2227: left of '->fname' must point to class/struct/union
Error executing cl.exe.
lab5.obj - 6 error(s), 0 warning(s)
-
cool fixed up the code, however theres something REALLY REALLY REALLY WEIRD, possibly my compiler, for some reason if i dont have the part with the enter number at the start i get faults happening when the file is read in and it gets to the balance
Code:
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER *tempptr;
double balance;
//FOR SOME WEIRD ARSE REASON WITHOUT THIS IT CRASHES, ANY IDEAS ANYONE?
printf("press a number else it doesnt work, no idea why");
scanf("%lf",&balance);
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN ALL THE FILE INTO THE LIST
while(!feof(infile))
{
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else
{
tempptr = bankrecords;
while(tempptr->next!=NULL)
tempptr=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
fscanf(infile,"%s %s %c %D %lf",tempptr->fname,tempptr->lname,&(tempptr->initial),&(tempptr->socialnum),&(tempptr->balance));
tempptr->next=NULL;
}
fclose(infile);
// PRINT ALL THE LIST
tempptr = bankrecords;
printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
printf("-----------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%s %c %s %ld %.2lf\n",tempptr->fname,tempptr->initial,tempptr->lname,tempptr->socialnum,tempptr->balance);
tempptr=tempptr->next;
}
}
return 1;
}
oviously you'll need to format the output, ie to do the account number properly do %15d and it will right align (i think) everything giving 15 spaces.
also if you need to free up the memory yourself rather than letting the os do it then I suggest making it a double linked list, heaps easier to free large amounts of memory and quicker.
Hope this helps:)
-
<quote>code
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER *tempptr;
clrscr();
/*double balance;*/
/*FOR SOME WEIRD ARSE REASON WITHOUT THIS IT CRASHES, ANY IDEAS ANYONE?*/
/*printf("press a number else it doesnt work, no idea why");
scanf("%lf",&balance);*/
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
/* READ IN ALL THE FILE INTO THE LIST*/
while(!feof(infile))
{
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else
{
tempptr = bankrecords;
while(tempptr->next!=NULL)
tempptr=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
fscanf(infile,"%s %s %c %D %lf",tempptr->fname,tempptr->lname,&(tempptr->initial),&(tempptr->socialnum),&(tempptr->balance));
tempptr->next=NULL;
}
fclose(infile);
/* PRINT ALL THE LIST */
tempptr = bankrecords;
printf("LAST\tMI\tFIRST\t SSN \t\tBALANCE \t\n");
printf("-----------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%s\t %c\t %s\t %ld\t %.2lf\n",tempptr->fname,tempptr->initial,tempptr->lname,tempptr->socialnum,tempptr->balance);
tempptr=tempptr->next;
}
}
getch();
return 1;
}
</quote>
the code is working without reading that double value, Bull. I think you better use a good ol C compiler, rather than a state-of-the art new win32 CPP compiler. You can find one such nice compiler at http://community.borland.com.
Borland is giving away its TurboC 2.1 compiler for free. All u need to do is register there and download it from their Museum.
Note: i made some changes in printf's which will be working fine on my system.
--cheers.
-
I need the data to print right because the data file im reading from is not formatted so it might have something like:
Jones Mike K 857102563 214.36
Matty Joe L 142036987 1487.24
So when i output the data it does not line up correctly.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
int socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER *tempptr;
int track=1;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
while(!feof(infile))
{
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else
{
tempptr = bankrecords;
while(tempptr->next!=NULL)
tempptr=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
fscanf(infile,"%s %s %c %d %lf",&tempptr->lname,&tempptr->fname,&tempptr->initial,&tempptr->socialnum,&tempptr->balance);
tempptr->next=NULL;
}
fclose(infile);
// PRINT ALL THE LIST
tempptr = bankrecords;
printf("NUM\tADDRESS LAST\tFIRST MI SSN BALANCE NEXT\n");
printf("-----------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%d %8X\t %s \t%s %c %d %.2lf %8X\n",track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance,tempptr->next);
track++;
tempptr=tempptr->next;
}
}
return 0;
}
-
If it doesn't line up right when you output it, you need to actually learn how to use printf:
printf("%8d", 10 ); <--- will make this number take 8 spaces on the screen (unless it's longer than that)
There are similar notations for all forms:
%Ns
%Nc
%Nd
%Nf
Replace N with a number.
Quzah.
-
ok as of right now if my input file has the following:
Johnson Mike J 253657410 19473.00
Mack Johnny T 230459630 21045.00
Jackson Lewis J 069741052 25.69
And i run my program my output does this:
Code:
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
----------------------------------------------------------------
1 430090 Johnson Mike J 253657410 19473.00 431E60
2 431E60 Mack Johnny T 230459630 21045.00 431DF0
3 431DF0 Jackson Lewis J 69741052 25.69 0
Press any key to continue
how can i get it to print like this:
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
----------------------------------------------------------------
1 430090 Johnson Mike J 253657410 19473.00 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
----------------------------------------------------------------
1 430090 Johnson Mike J 253657410 19473.00 431E60
2 431E60 Mack Johnny T 230459630 21045.00 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
----------------------------------------------------------------
1 431DF0 Jackson Lewis J 69741052 25.69 431E62
2 430090 Johnson Mike J 253657410 19473.00 431E60
3 431E60 Mack Johnny T 230459630 21045.00 NULL
It has to sort the names by alphabetical order by last names and if last names are the same go by first names.
-
oh yeah here is my code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
int socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER *tempptr;
int track=1;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
while(!feof(infile))
{
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else
{
tempptr = bankrecords;
while(tempptr->next!=NULL)
tempptr=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
fscanf(infile,"%s %s %c %d %lf",&tempptr->lname,&tempptr->fname,&tempptr->initial,&tempptr->socialnum,&tempptr->balance);
tempptr->next=NULL;
}
fclose(infile);
// PRINT ALL THE LIST
tempptr = bankrecords;
printf("NUM\tADDRESS LAST\t \tFIRST MI SSN BALANCE NEXT\n");
printf("----------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%d %8X\t%-12s \t%-9s %1c %9d %.2lf %8X\n",track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance,tempptr->next);
track++;
tempptr=tempptr->next;
}
return 0;
}
-
Code:
while(tempptr!=NULL)
{
printf("%d %8X\t %s \t%s %c %d %.2lf %8X\n",track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance,tempptr->next);
track++;
tempptr=tempptr->next;
}
Modify it to something like:
Code:
while(tempptr!=NULL)
{
printf("%d %8X\t %s \t%s %c %d %.2lf ",
track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance );
if( tempptr->next == NULL )
printf( " NULL\n" );
else
printf("%8d\n", tempptr->next );
track++;
tempptr=tempptr->next;
}
Quzah.
-
ok but what about getting it to print out the way i showed in my last 2 posts?
-
It's a very simple looping process:
Code:
while( ptr1 != NULL )
{
print header text
while( ptr2 != ptr1 )
{
print ptr2 infor
ptr2 = ptr2->next;
}
print ptr1 information
ptr1 = ptr1->next;
}
Should do the trick.
Quzah.
-
im not understanding what you typed. I mean i sort of understand but not completely
-
ok the easiest way to do that is probably to sort it as its being inserted into the list and then just print them out as normal, below is the code i posted which your using now but modified which sorts the list on insertion:
Code:
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN ALL THE FILE INTO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
tempptr=tempptr->next;
if(bankrecords==tempptr) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord.fname);
strcpy(tempptr->lname,temprecord.lname);
tempptr->initial = temprecord.initial;
tempptr->socialnum = temprecord.socialnum;
tempptr->balance = temprecord.balance;
}
fclose(infile);
// PRINT ALL THE LIST
tempptr = bankrecords;
printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
printf("-----------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%s %c %s %ld %.2lf\n",tempptr->fname,tempptr->initial,tempptr->lname,tempptr->socialnum,tempptr->balance);
tempptr=tempptr->next;
}
}
return 1;
}
you can either cut bits out or create 2 new variables as:
Code:
CUSTOMER *tempptr2;
CUSTOMER temprecord;
and then change the code within the while(!feof(infile)) to
Code:
while(!feof(infile))
{
fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
tempptr=tempptr->next;
if(bankrecords==tempptr) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord.fname);
strcpy(tempptr->lname,temprecord.lname);
tempptr->initial = temprecord.initial;
tempptr->socialnum = temprecord.socialnum;
tempptr->balance = temprecord.balance;
}
hope this helps
-
ok I am continuing to have issues with printing out each record one at a time. When ever i modify my code i can never get it go print out one at a time instead it prints out all of them.
The data i am using for input looks like this:
Johnson Mike J 253657410 19473.00
Mack Johnny T 230459630 21045.00
Jackson Lewis J 169741052 2579.69
Zunny Jack L 065523069 25144.36
Ashberry Jim P 741803205 26390.54
i want it to first print out the first persons info like this:
Code:
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------------
1 431DB0 Ashberry Jim P 741803205 26390.54 NULL
Then print out:
Code:
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------------
1 431DB0 Ashberry Jim P 741803205 26390.54 431E90
2 431E90 Jackson Lewis J 169741052 2579.69 NULL
Then print:
Code:
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------------
1 431DB0 Ashberry Jim P 741803205 26390.54 431E90
2 431E90 Jackson Lewis J 169741052 2579.69 431F00
3 431F00 Mack Johnny T 230459630 21045.00 NULL
And so on.....
what am i doing wrong?
-
you basically need a counter and loop through until you are at the end, so through the first pass it will print 1, then 2, and so on, also I screwed a little of the code b4, below is all the code, works perfect on my pc, and sorts them properly now:
Code:
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
CUSTOMER temprecord;
int counter1,counter2;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN ALL THE FILE INTO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
tempptr=tempptr->next;
if((bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord.fname)>0)) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord.fname);
strcpy(tempptr->lname,temprecord.lname);
tempptr->initial = temprecord.initial;
tempptr->socialnum = temprecord.socialnum;
tempptr->balance = temprecord.balance;
}
fclose(infile);
// PRINT ALL THE LIST
tempptr = bankrecords;
for(counter1=0;tempptr!=NULL;counter1++)
{
tempptr = bankrecords;
printf("NUM\tADDRESS\tLAST\tFIRST\tMI SSN BALANCE NEXT\n");
printf("----------------------------------------------------------------\n\n");
for(counter2=0;counter2<=counter1;counter2++)
{
printf("%d%5X\t%-12s\t%-9s\t%c %9ld %.2lf ",counter2+1,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf("NULL\n");
else
printf("%5X\n",tempptr->next);
tempptr=tempptr->next;
}
printf("\n\n");
}
}
return 1;
}
hope this solves ya problem.
-
Thank you that helped alot but when i try to move the print part of my code inside my while loop where i read in the data it gives me errors. I am trying to get it where after it reads one line of the file which is one customer to print out the contents of the linked list so far then it should go back and read in the next line then print the contents of the linked list which would be 2 customers and so on.
here my error ridden code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER,LIST;
int main()
{
FILE *infile,*infile2;
CUSTOMER *bankrecords;
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
CUSTOMER temprecord;
LIST *bankrecords2;
LIST *tempptr3;
LIST *tempptr4;
LIST temprecord2;
int counter1,counter2,track=1;;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN ALL THE FILE INTO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %D %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
for(counter1=0;tempptr!=NULL;counter1++) {
tempptr = bankrecords;
printf("NUM\tADDRESS LAST\t \tFIRST MI SSN BALANCE NEXT\n");
printf("-------------------------------------------------------------------------------\n\n");
track=1;
for(counter2=0;counter2<=counter1;counter2++,track++) {
printf("%d %8X\t%-12s \t%-9s %1c %9d %-.2lf ",track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf("NULL\n");
else
printf("%6X\n",tempptr->next);
tempptr=tempptr->next;
}
printf("\n\n");
}
}
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
tempptr=tempptr->next;
if((bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord.fname)>0)) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
// i tried to move this inside the while loop but it did no good
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord.fname);
strcpy(tempptr->lname,temprecord.lname);
tempptr->initial = temprecord.initial;
tempptr->socialnum = temprecord.socialnum;
tempptr->balance = temprecord.balance;
}
fclose(infile);
}
return 0;
}
-
This is easy. Break this into seperate functions. You are cluttering this main function up horribly. The easiest way to do this:
Create a function that reads 1 entry and puts it in the list:
int readRecord( FILE *fp, struct MyList *list )
{
...if file
......newNode = allocate a new node
......read into newnode
......append new node to list
......return success
...return failure
}
Create a function that displays the list:
void displayList( struct MyList *list )
{
...print header...
...while !end of list
......display this node
......node = next node
}
Use this main function:
int main ( void )
{
...open file
...while readRecord( file ) != failure
......displayList
...close file
...return success
}
See how easy it is when you break it down into logical steps?
Quzah.
-
the easiest way if you dont want to create function which may be an idea once its working is to grab the code I posted before and change it so at the bottom of the for statement before:
put:
Code:
// PRINT ALL THE LIST
tempptr = bankrecords;
printf("NUM\tADDRESS\tLAST\tFIRST\tMI SSN BALANCE NEXT\n");
printf("----------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%d%5X\t%-12s\t%-9s\t%c %9ld %.2lf ",counter2+1,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf("NULL\n");
else
printf("%5X\n",tempptr->next);
tempptr=tempptr->next;
}
printf("\n\n");
}
fclose(infile);
and then delete the printouts later in the code and should all work how ya want it too.
-
Ok you have lost me i understand putting it into functions but im confused as to what declarations should go in which function.
-
How hard is this to understand?
1) Make a function to read from a file pointer, which puts that informtion on the end of the list.
You already know how to do this, so put it in a function so that this is ALL that the function does.
a) read the line
b) if EOF, return failure
c) else create a node and put the info in it
2) Make a function to print the list.
You already know how to do this also.
Pass it the list, cycle through it and display it all.
Quzah.
-
Since this post has been goin on for ages and you have actually been trying different things etc. I've fixed it up, but I wouldnt normally, here it is in function, make sure you read it and understand it before you hand it in for your subject.
P.S. you'll have to change the print statements to whatever ones your using.
Code:
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord->fname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord->fname)>0)) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
tempptr->initial = temprecord->initial;
tempptr->socialnum = temprecord->socialnum;
tempptr->balance = temprecord->balance;
}
void printlist(CUSTOMER **bankrecords)
{
CUSTOMER *tempptr;
int counter=1;
tempptr = *bankrecords;
printf("NUM\tADDRESS\tLAST\tFIRST\tMI SSN BALANCE NEXT\n");
printf("----------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%d%5X\t%-12s\t%-9s\t%c %9ld %.2lf ",counter,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf("NULL\n");
else
printf("%5X\n",tempptr->next);
tempptr=tempptr->next;
counter++;
}
printf("\n\n");
}
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN FILE TO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
fclose(infile);
}
return 1;
}
-
oops that last line
should be
-
RE: "Oops"
You do know, Bull, that you can click "edit" and go back and modify your posts, right? Just thought I'd let you know. I see tons of people that post "oops" follow-up posts when they could have just edited their original.
Quzah.
-
ok i have read the data in from one file and there is a second file that has some more info and i am going to read one piece in at a time and then do some things with it according to exactly what it is. So my question is this: How am i reading in the second file? am i starting another linked list or is it going in the same list? Also say i want to compare the SSN from teh first list with one from the second list to see if the customer is already on that list how could i do that? My problem is in the main function.
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
char socialnum[10];
double balance;
double amount;
struct customer *next;
}CUSTOMER;
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord->fname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord->fname)>0)) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
tempptr->initial = temprecord->initial;
strcpy(tempptr->socialnum,temprecord->socialnum);
tempptr->balance = temprecord->balance;
}
void addtolist2(CUSTOMER **transactions,CUSTOMER *temp)
{
CUSTOMER *tempptr3;
CUSTOMER *tempptr4;
if(*transactions==NULL)
{
*transactions = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr3 = *transactions;
tempptr3->next=NULL;
}
else
{
tempptr3 = *transactions;
while((tempptr3->next!=NULL) && (strcmp(tempptr3->fname,temp->fname)<0))
tempptr3=tempptr3->next;
if((*transactions==tempptr3) && (strcmp(tempptr3->fname,temp->fname)>0))
{
tempptr4=tempptr3;
*transactions = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr3 = *transactions;
}
else
{
tempptr4=tempptr3->next;
tempptr3->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr3=tempptr3->next;
}
tempptr3->next=tempptr4;
}
strcpy(tempptr3->fname,temp->fname);
strcpy(tempptr3->lname,temp->lname);
tempptr3->initial = temp->initial;
strcpy(tempptr3->socialnum,temp->socialnum);
tempptr3->balance = temp->balance;
}
void printlist(CUSTOMER **bankrecords)
{
CUSTOMER *tempptr;
int counter=1;
tempptr = *bankrecords;
printf("NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT\n");
printf("----------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%d %5X %-12s%-9s%c %s %.2lf ",counter,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf(" NULL\n");
else
printf("%5X\n",tempptr->next);
tempptr=tempptr->next;
counter++;
}
printf("\n\n");
}
int main()
{
FILE *infile,*infile2;
CUSTOMER *bankrecords;
CUSTOMER temprecord;
CUSTOMER *transactions;
CUSTOMER temp;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN FILE TO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
}
infile2=fopen("data_2.dat","r");
/////This is the part in Question!!!!///
if(infile2 == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
while(!feof(infile2))
{
fscanf(infile,"%s %s %c %s %lf",temp.lname,temp.fname,&(temp.initial),temp.socialnum,&(temp.amount));
}
}
return 0;
}
-
ok the following code wont allow 2 occurences of the same SSN in the same list:
Code:
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
// RETURN 1 IF INSERTION IS SUCCESSFUL
int addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord->fname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord->fname)>0)) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=tempptr2;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
if(tempptr->socialnum != temprecord->socialnum)
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
tempptr->next=tempptr2;
}
else return 0;
}
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
tempptr->initial = temprecord->initial;
tempptr->socialnum = temprecord->socialnum;
tempptr->balance = temprecord->balance;
return 1;
}
void printlist(CUSTOMER **bankrecords)
{
CUSTOMER *tempptr;
int counter=1;
tempptr = *bankrecords;
printf("NUM\tADDRESS\tLAST\tFIRST\tMI SSN BALANCE NEXT\n");
printf("----------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%d%5X\t%-12s\t%-9s\t%c %9ld %.2lf ",counter,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf("NULL\n");
else
printf("%5X\n",tempptr->next);
tempptr=tempptr->next;
counter++;
}
printf("\n\n");
}
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN FILE TO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
if(addtolist(&bankrecords,&temprecord))
printlist(&bankrecords);
}
fclose(infile);
}
return 0;
}
as long as the second file is being read in with the same information then theres no reason you cant use the same list. just read each line in the same (saving it into temprecord) and then do whatever checks and stuff ya need to and save it if you want to, alternatevly you could create a new list just as easy by doing this:
Code:
CUSTOMER *secondlist=NULL;
and then using the functions exactly the same way.
Hope this helps
-
Ok once i read in the data from the second file the first thing im trying to do is compare the social num i just read in to the ones already in the list to see if there is a match, how can i do that?
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
char socialnum[10];
double balance;
double amount;
struct customer *next;
}CUSTOMER;
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->lname,temprecord->lname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord->fname)>0)) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
tempptr->initial = temprecord->initial;
strcpy(tempptr->socialnum,temprecord->socialnum);
tempptr->balance = temprecord->balance;
}
void printlist(CUSTOMER **bankrecords)
{
CUSTOMER *tempptr;
int counter=1;
tempptr = *bankrecords;
printf("NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT\n");
printf("----------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%d %5X %-12s%-9s%c %s %.2lf ",counter,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf(" NULL\n");
else
printf("%5X\n",tempptr->next);
tempptr=tempptr->next;
counter++;
}
printf("\n\n");
}
int main()
{
FILE *infile,*infile2;
CUSTOMER *bankrecords;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN FILE TO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
if(strcmp(temprecord.socialnum
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
}
infile=fopen("data_2.dat","r");
if(infile2 == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
while(!feof(infile))
{
fscanf(infile,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.amount));
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
}
return 0;
}
-
something like this should work:
Code:
// Returns true (1) if the record exists
int findinlist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
tempptr = *bankrecords;
if(tempptr==NULL)
return 0;
else
{
while((tempptr!=NULL) && (tempptr->socialnum!=temprecord->socialnum))
tempptr=tempptr->next;
if(tempptr==NULL)
return 0;
else
return 1;
}
}
should work, havent tested it, I see your using a string for socialnum, so in that case you whould have the while statement reading something like:
Code:
while((tempptr!=NULL) && (strcomp(tempptr->socialnum,temprecord->socialnum)<>0))
but if you are only going to be inserting it into the list if its not there then you can just use the add function I fixed as it returns 0
if the record is already in the list.
-
Sort
ok my problem is that when i expanded my list of names from the first file. As it reads each person and their stats to teh linked list it no longer sorts them by their last name?!! I have no idea why it did that.
Here is the exact data file im using
Springston Ray L 374926490 40392.37
Heineman Doug A 264592791 19473.08
Alagappan Solayappan Q 567493784 20493.76
Easton Dwight R 193465943 30284.33
Chatila Sami K 029375938 1943.88
Johnson Devin E 736549283 197493.93
Fazal Talha S 643847651 2947.08
Punjal Jagan D 463248598 29473.71
Pruitt Terry O 846239402 19374.50
Ma Glen T 264957640 134.29
Lewis Troy R 002947594 19374.12
Khan Eddy U 947604592 9476.66
Tran Michael B 204946731 652.09
Hopkins Weston S 463947204 2940.45
Code:
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else /* RECORDS ALREADY EXIST */
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->lname,temprecord->lname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord->fname)>0)) /* RECORD BELONGS AT START OF LIST */
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
}
else /* RECORD IS SOMEWHERE IN MIDDLE OR END */
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
/* COPY ALL DATA FROM TEMP RECORD TO NEW ONE */
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
tempptr->initial = temprecord->initial;
strcpy(tempptr->socialnum,temprecord->socialnum);
tempptr->balance = temprecord->balance;
}
what could be wrong?
-
ok the problem is that it originally sorted them by their firstname, and in the addtolist function theres still some references to fname, all you have to do is search through the addtolist function and change EVERYTHING that says fname to lname. and that should fix your problem.
-
ok i went back and changed them all to last name as you can see below:
Code:
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else /* RECORDS ALREADY EXIST */
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->lname,temprecord->lname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->lname,temprecord->lname)>0)) /* RECORD BELONGS AT START OF LIST */
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
}
else /* RECORD IS SOMEWHERE IN MIDDLE OR END */
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
/* COPY ALL DATA FROM TEMP RECORD TO NEW ONE */
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
tempptr->initial = temprecord->initial;
strcpy(tempptr->socialnum,temprecord->socialnum);
tempptr->balance = temprecord->balance;
}
....but my output is still this:
Code:
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 430080 Springston Ray L 374926490 40392.37 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431E50 Heineman Doug A 264592791 19473.08 430080
2 430080 Springston Ray L 374926490 40392.37 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 430080
3 430080 Springston Ray L 374926490 40392.37 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431D50
3 431D50 Easton Dwight R 193465943 30284.33 430080
4 430080 Springston Ray L 374926490 40392.37 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431CD0
3 431CD0 Chatila Sami K 029375938 1943.88 431D50
4 431D50 Easton Dwight R 193465943 30284.33 430080
5 430080 Springston Ray L 374926490 40392.37 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431CD0
3 431CD0 Chatila Sami K 029375938 1943.88 431D50
4 431D50 Easton Dwight R 193465943 30284.33 430080
5 430080 Springston Ray L 374926490 40392.37 431C50
6 431C50 Johnson Devin E 736549283 197493.93 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431BD0
3 431BD0 Fazal Talha S 643847651 2947.08 431CD0
4 431CD0 Chatila Sami K 029375938 1943.88 431D50
5 431D50 Easton Dwight R 193465943 30284.33 430080
6 430080 Springston Ray L 374926490 40392.37 431C50
7 431C50 Johnson Devin E 736549283 197493.93 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431BD0
3 431BD0 Fazal Talha S 643847651 2947.08 431CD0
4 431CD0 Chatila Sami K 029375938 1943.88 431D50
5 431D50 Easton Dwight R 193465943 30284.33 430080
6 430080 Springston Ray L 374926490 40392.37 431B50
7 431B50 Punjal Jagan D 463248598 29473.71 431C50
8 431C50 Johnson Devin E 736549283 197493.93 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431BD0
3 431BD0 Fazal Talha S 643847651 2947.08 431CD0
4 431CD0 Chatila Sami K 029375938 1943.88 431D50
5 431D50 Easton Dwight R 193465943 30284.33 430080
6 430080 Springston Ray L 374926490 40392.37 431AD0
7 431AD0 Pruitt Terry O 846239402 19374.50 431B50
8 431B50 Punjal Jagan D 463248598 29473.71 431C50
9 431C50 Johnson Devin E 736549283 197493.93 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431BD0
3 431BD0 Fazal Talha S 643847651 2947.08 431CD0
4 431CD0 Chatila Sami K 029375938 1943.88 431D50
5 431D50 Easton Dwight R 193465943 30284.33 430080
6 430080 Springston Ray L 374926490 40392.37 431A50
7 431A50 Ma Glen T 264957640 134.29 431AD0
8 431AD0 Pruitt Terry O 846239402 19374.50 431B50
9 431B50 Punjal Jagan D 463248598 29473.71 431C50
10 431C50 Johnson Devin E 736549283 197493.93 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431BD0
3 431BD0 Fazal Talha S 643847651 2947.08 431CD0
4 431CD0 Chatila Sami K 029375938 1943.88 431D50
5 431D50 Easton Dwight R 193465943 30284.33 430080
6 430080 Springston Ray L 374926490 40392.37 4319D0
7 4319D0 Lewis Troy R 002947594 19374.12 431A50
8 431A50 Ma Glen T 264957640 134.29 431AD0
9 431AD0 Pruitt Terry O 846239402 19374.50 431B50
10 431B50 Punjal Jagan D 463248598 29473.71 431C50
11 431C50 Johnson Devin E 736549283 197493.93 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431BD0
3 431BD0 Fazal Talha S 643847651 2947.08 431CD0
4 431CD0 Chatila Sami K 029375938 1943.88 431D50
5 431D50 Easton Dwight R 193465943 30284.33 430080
6 430080 Springston Ray L 374926490 40392.37 431950
7 431950 Khan Eddy U 947604592 9476.66 4319D0
8 4319D0 Lewis Troy R 002947594 19374.12 431A50
9 431A50 Ma Glen T 264957640 134.29 431AD0
10 431AD0 Pruitt Terry O 846239402 19374.50 431B50
11 431B50 Punjal Jagan D 463248598 29473.71 431C50
12 431C50 Johnson Devin E 736549283 197493.93 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431BD0
3 431BD0 Fazal Talha S 643847651 2947.08 431CD0
4 431CD0 Chatila Sami K 029375938 1943.88 431D50
5 431D50 Easton Dwight R 193465943 30284.33 430080
6 430080 Springston Ray L 374926490 40392.37 431950
7 431950 Khan Eddy U 947604592 9476.66 4319D0
8 4319D0 Lewis Troy R 002947594 19374.12 431A50
9 431A50 Ma Glen T 264957640 134.29 431AD0
10 431AD0 Pruitt Terry O 846239402 19374.50 431B50
11 431B50 Punjal Jagan D 463248598 29473.71 431C50
12 431C50 Johnson Devin E 736549283 197493.93 4318D0
13 4318D0 Tran Michael B 204946731 652.09 NULL
NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT
-------------------------------------------------------------------------
1 431DD0 Alagappan Solayappan Q 567493784 20493.76 431E50
2 431E50 Heineman Doug A 264592791 19473.08 431BD0
3 431BD0 Fazal Talha S 643847651 2947.08 431CD0
4 431CD0 Chatila Sami K 029375938 1943.88 431D50
5 431D50 Easton Dwight R 193465943 30284.33 430080
6 430080 Springston Ray L 374926490 40392.37 431850
7 431850 Hopkins Weston S 463947204 2940.45 431950
8 431950 Khan Eddy U 947604592 9476.66 4319D0
9 4319D0 Lewis Troy R 002947594 19374.12 431A50
10 431A50 Ma Glen T 264957640 134.29 431AD0
11 431AD0 Pruitt Terry O 846239402 19374.50 431B50
12 431B50 Punjal Jagan D 463248598 29473.71 431C50
13 431C50 Johnson Devin E 736549283 197493.93 4318D0
14 4318D0 Tran Michael B 204946731 652.09 NULL
Press any key to continue
I have no clue why its doing this
-
fixed, acidently screwed up the search function to work out where it goes, before it was checking the current lists lname with the value to insert, if it was bigger then it put it after it instead of before it. So instead I made it check the next->lname value instead:
Code:
// RETURN 1 IF INSERTION IS SUCCESSFUL
int addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->next->lname,temprecord->lname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->next->lname,temprecord->lname)>0)) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=tempptr2;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
if(tempptr->socialnum != temprecord->socialnum)
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
tempptr->next=tempptr2;
}
else return 0;
}
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
tempptr->initial = temprecord->initial;
tempptr->socialnum = temprecord->socialnum;
tempptr->balance = temprecord->balance;
return 1;
}
-
ok im trying to set up a way for me to be able to make adjustments to my list, i have started but its beginning to get confusing to me. im tyring to set up a switch statement or something where like
If the Social Security number does NOT already exist in the linked list, AND
the transaction request is a DEPOSIT, then
1. print out a statement letting them know they are being added
If the Social Security number does NOT already exist in the linked list, AND
the transaction request is a WITHDRAWAL, then
1. print out statement saying they do not exist in the list
If the Social Security number DOES already exist in the linked list, AND the
transaction request is a DEPOSIT, then
1. print out statement saying that the amount is being added
If the Social Security number DOES already exist in the linked list, AND the
transaction request is a WITHDRAWAL, and the amount to be withdrawn does NOT
exceed the balance, then
1. statement saying they are making a withdrawal
If the Social Security number DOES already exist in the linked list, AND the
transaction request is a WITHDRAWAL, AND the amount to be withdrawn exceeds
the balance, then
1.message saying their acount will be close and then delete the account.
How can i go about this.
Code:
int findinlist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
int test;
tempptr = *bankrecords;
if(tempptr==NULL)
return 0;
else
{
while((tempptr!=NULL) && (atoi(tempptr->socialnum)!=atoi(temprecord->socialnum))) {
tempptr=tempptr->next;
}
if((temprecord->balance) > 0 )
test=1;
if((temprecord->balance < 0 ) && (temprecord->balance < tempptr->balance))
test=2;
if(tempptr==NULL) {
return 0;
}
if(test==0)
printf("Error");
else if(test==1) {
printf("%s %c %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 1;
}
else if (test==2) {
printf("%s %c %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
tempptr->balance = tempptr->balance - temprecord->balance;
printlist(bankrecords);
return 1;
}
}
}
-
I think the best way is to make a function as you have their called exists in list, which returns say 1 if they do 0 if they dont, also to modify the addtolist function so that if the record exists then it checks if its a deposit, etc and does it to the list and returns a specific value depending on what the condition of the add request is, only problem is the addtolist function may come fairly big.
Anyway the checkinlist function should look somethin like:
Code:
int checkinlist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
tempptr = *bankrecords;
if(tempptr!=NULL)
while((tempptr!=NULL) && (tempptr->socialnum!=temprecord->socialnum))
tempptr=tempptr->next;
if(tempptr==NULL)
return 0;
else
return 1;
}
that should be fine to return 1 if the record exists and 0 if it doesnt.
-
im have problems with my code, it checks the second file for people who want to make a deposit and prints out a statement and also for those who have an account and want a withdrawal but it does not work for the rest of my cases can someone tell me what is wrong?
Code:
int findinlist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
int test;
tempptr = *bankrecords;
if(tempptr==NULL)
return 0;
else
{
while((tempptr!=NULL) && (atoi(tempptr->socialnum)!=atoi(temprecord->socialnum))) {
tempptr=tempptr->next;
}
if((temprecord->balance) > 0 )
test=1;
if((temprecord->balance < 0 ) && (temprecord->balance < tempptr->balance))
test=2;
if((temprecord->balance < 0 ) && (temprecord->balance > tempptr->balance))
test=3;
if(tempptr==NULL) {
return 0;
}
if(test==0)
printf("Error");
else if(test==1) {
printf("%s %c %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 1;
}
else if (test==2) {
printf("%s %c %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
tempptr->balance = tempptr->balance - temprecord->balance;
printlist(bankrecords);
}
else if(test==3) {
printf("%s %c %s, %s, is attempting a withdrawal of $%.2lf the account does not exist.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
printlist(bankrecords);
}
}
}
-
what other conditions are there that you need to test for?
-
These are the conditions i need to test for:
If the Social Security number does NOT already exist in the linked list, AND
the transaction request is a DEPOSIT, then
1. print out a statement letting them know they are being added
If the Social Security number does NOT already exist in the linked list, AND
the transaction request is a WITHDRAWAL, then
1. print out statement saying they do not exist in the list
If the Social Security number DOES already exist in the linked list, AND the
transaction request is a DEPOSIT, then
1. print out statement saying that the amount is being added
If the Social Security number DOES already exist in the linked list, AND the
transaction request is a WITHDRAWAL, and the amount to be withdrawn does NOT
exceed the balance, then
1. statement saying they are making a withdrawal
If the Social Security number DOES already exist in the linked list, AND the
transaction request is a WITHDRAWAL, AND the amount to be withdrawn exceeds
the balance, then
1.message saying their acount will be close and then delete the account
-
ok for the first 2, you could check the returned value, if it returns 0 then you know the record wasnt found, so they dont have an account, and then check if the transaction type is a withdrawl or deposit and do whatever accordingly. If you want to do it inside the findinlist function then pass the transaction type to the function and inside this piece of code:
Code:
if(tempptr==NULL) {
return 0;
}
put in whatever you want it to do (before the return 0) so it could be like:
Code:
if(tempptr==NULL // No account exists
{
if(transactiontype='W')
{
printf("You dont have an account to create\n");
return 4;
}
else
{
printf("Depositing money in new account");
addtolist(*bankrecords, temprecord);
return 5;
}
}
and that should handle all new accounts.
-
what about deleting an account. if i have someone who's social is is inthe list but the amount they are trying to withdraw is more than their current balance i need to delete them.
-
ok for some reason its not adding the amounts correctly.
I just tested the code with one file which is the first input file :
Springston Ray L 374926490 40392.37
Heineman Doug A 264592791 19473.08
Alagappan Solayappan Q 567493784 20493.76
Easton Dwight R 193465943 30284.33
Chatila Sami K 029375938 1943.88
Johnson Devin E 736549283 197493.93
Fazal Talha S 643847651 2947.08
Punjal Jagan D 463248598 29473.71
Pruitt Terry O 846239402 19374.50
Ma Glen T 264957640 134.29
Lewis Troy R 002947594 19374.12
Khan Eddy U 947604592 9476.66
Tran Michael B 204946731 652.09
Hopkins Weston S 463947204 2940.45
The data above was stored in my linked list and then i read from another file with 2 records in it and its not working.
Ma Glen T 264957640 +14932.47
Khan Eddy U 947604592 -80.50
what could possibly be wrong i have the code right i think :
Code:
int findssn(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
tempptr = *bankrecords;
if(tempptr!=NULL)
while((tempptr!=NULL) && (atoi(tempptr->socialnum)!=atoi(temprecord->socialnum))) {
tempptr=tempptr->next;
}
if(tempptr==NULL)
return 0;
else
return 1;
}
int deposit(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
tempptr = *bankrecords;
if((temprecord->balance) > 0 )
{
printf("%s %c %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if(((temprecord->balance) < 0 ) && ((fabs(temprecord->balance) < tempptr->balance)))
{
printf("%s %c %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if((fabs(temprecord->balance) < tempptr->balance))
printf("%s %c %s, %s, is attempting a withdrawal of $%.2lf the account but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
printlist(bankrecords);
return 0;
}
int deposit2(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
tempptr = *bankrecords;
if((temprecord->balance) > 0 )
{
printf("%s %c %s, %s, is being added to the list with a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
addtolist(bankrecords,temprecord);
printlist(bankrecords);
return 0;
}
else {
printf("%s %c %s, %s, is attempting a withdrawal of $%.2lf the account does not exist.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
printlist(bankrecords);
return 0;
}
}
int main()
{
FILE *infile,*infile2;
CUSTOMER *bankrecords;
CUSTOMER *secondlist=NULL;
int test;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
/* READ IN FILE TO THE LIST */
while(!feof(infile))
{
fscanf(infile,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
}
infile2=fopen("data_2.dat","r");
if(infile2 == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
while(!feof(infile2))
{
fscanf(infile2,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
test=findssn(&bankrecords,&temprecord);
if(test==1) {
test=deposit(&bankrecords,&temprecord);
}
else
test=deposit2(&bankrecords,&temprecord);
}
}
return 0;
}
-
how can i delete a entire record from my linked list?
-
ok the way you have to do it, without using a double linked list, is to find the record you need without actually going to it, ie:
if(tempptr->next->socialnum = temprecord->socailnum)
or whatever it is then you would have another tempptr, ie:
Code:
CUSTOMER *tempptr2;
// CHECK HERE IF THE FIRST ITEM IS THE ONE YA LOOKIN FOR: IE
if(tempptr->socialnum == temprecord->socialnum)
{
if(transaction='W' && money<requested)
{
tempptr2=tempptr;
tempptr=tempptr->next;
free(tempptr2);
}
}
else
{
while((tempptr->next->socialnum != temprecord->socailnum) && (tempptr->next != NULL))
tempptr=tempptr->next;
if(transaction='W' && money<requested)
{
if(tempptr->next != NULL)
{
tempptr2 = tempptr->next;
tempptr->next = tempptr->next->next;
free(tempptr2);
}
}
something like that should work fine, you just have to make sure once you reconnect the linked list around the one you want to delete, that you still have a pointer connected to the one you want to delete, thats what tempptr2 is for.
-
I have been having problems with the free command since forever here is my code :
Code:
if((temprecord->balance < tempptr->balance))
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
if(tempptr->socialnum == temprecord->socialnum)
{
tempptr2=tempptr;
tempptr=tempptr->next;
free(tempptr2);
}
else
{
while((tempptr->next->socialnum != temprecord->socialnum) && (tempptr->next != NULL))
tempptr=tempptr->next;
if(tempptr->next != NULL)
{
tempptr2 = tempptr->next;
tempptr->next = tempptr->next->next;
free(tempptr2);
}
}
For some reason its not deleting the record because after i print my message saying their account will be deleted and i print the contents of the list, that record is still there.
-
ok are you able to use double linked lists or not?
if not then you'll have to do a comparison like:
if((temprecord->balance < tempptr->next->balance))
because otherwise you have no connection with the record before the one you want to delete, draw a box diagram of say 5 records in the list and try to delete the 3rd one and reconnect up the linked list by assigning **** to pointers, if you find the record like above then the following code will work:
Code:
tempptr2 = tempptr->next; // Assign the memory to delete to tempptr2
tempptr = tempptr->next->next;
free(tempptr2);
this will delete one record from the middle of the list, you'll have to add more code to delete from the start of the list though.
Hope this helps.
-
so i can use the your other post if i have a double linked list?
so would my struct look like this:
Code:
typedef struct customer
{
char fname[20];
char lname[20];
char initial[3];
char socialnum[10];
double balance;
struct customer *next;
struct customer *prior;
}CUSTOMER;
-
i need to delete the head in my findssn function down towards the bottom in the last if statement.
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial[3];
char socialnum[10];
double balance;
struct customer *next;
}CUSTOMER;
/****************************************************************************
PROGRAMMER : Eric Austin
LANGUAGE : C
CLASS : CSE 1320
PLATFORM : (OMEGA, UTARLG, CSE, etc.)
OS : (UNIX, VAX, Win Me, etc.)
COMPILER : g++ (for CSE 1325) or j2sdk (for CSE 4301)
ASSIGNMENT : Lab 5
ASSIGNED : week of Monday, March 25, 2002
DUE : week of Monday, April 15, 2002
FILED AS : eja7288Lab5.c
FILES USED : eja7288Lab5.h
CONCEPTS : structures,
file i/o,
command line parameters,
linked lists
WEIGHT : 8%
PURPOSE : Write a C program that will use a linked list of structures to
represent the customers of a bank.
****************************************************************************/
void printlist(CUSTOMER **bankrecords)
{
CUSTOMER *tempptr;
int counter=1;
tempptr = *bankrecords;
printf("NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT\n");
printf("-------------------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%2d %5X %-14s%-12s%s %8s %10.2lf ",counter,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf(" NULL\n");
else
printf("%8X\n",tempptr->next);
tempptr=tempptr->next;
counter++;
}
printf("\n\n");
}
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else /* RECORDS ALREADY EXIST */
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->next->lname,temprecord->lname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->lname,temprecord->lname)>0)) /* RECORD BELONGS AT START OF LIST */
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
}
else /* RECORD IS SOMEWHERE IN MIDDLE OR END */
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
/* COPY ALL DATA FROM TEMP RECORD TO NEW ONE */
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
strcpy(tempptr->initial,temprecord->initial);
strcpy(tempptr->socialnum,temprecord->socialnum);
tempptr->balance = temprecord->balance;
}
int findssn(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *prev;
prev=NULL;
tempptr = *bankrecords;
if(tempptr!=NULL)
while((tempptr!=NULL) && (atoi(tempptr->socialnum)!=atoi(temprecord->socialnum))) {
prev=tempptr;
tempptr=tempptr->next;
}
if(tempptr==NULL)
{
if((temprecord->balance) > 0 )
{
printf("%s %s %s, %s, is being added to the list with a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
addtolist(bankrecords,temprecord);
printlist(bankrecords);
return 0;
}
else
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf the account does not exist.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
printlist(bankrecords);
return 0;
}
}
if((temprecord->balance) > 0 )
{
printf("%s %s %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if(((temprecord->balance) < 0 ) && ((fabs(temprecord->balance) < tempptr->balance)))
{
printf("%s %s %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if((temprecord->balance < tempptr->balance))
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
if(tempptr == *bankrecords) {
tempptr = tempptr->next;
free(tempptr);
printlist(bankrecords);
}
else {
prev->next=tempptr->next;
free (tempptr);
printlist(bankrecords);
}
return 0;
}
}
int main()
{
FILE *infile,*infile2;
CUSTOMER *bankrecords;
CUSTOMER *secondlist=NULL;
int test;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
/* READ IN FILE TO THE LIST */
while(!feof(infile))
{
fscanf(infile,"%s %s %s %s %lf",temprecord.lname,temprecord.fname,(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
}
infile2=fopen("data_2.dat","r");
if(infile2 == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
while(!feof(infile2))
{
fscanf(infile2,"%s %s %s %s %lf",temprecord.lname,temprecord.fname,(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
test=findssn(&bankrecords,&temprecord);
}
}
return 0;
}
-
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial[3];
char socialnum[10];
double balance;
struct customer *next;
}CUSTOMER;
void printlist(CUSTOMER **bankrecords)
{
CUSTOMER *tempptr;
int counter=1;
tempptr = *bankrecords;
printf("NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT\n");
printf("-------------------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%2d %5X %-14s%-12s%s %8s %10.2lf ",counter,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf(" NULL\n");
else
printf("%8X\n",tempptr->next);
tempptr=tempptr->next;
counter++;
}
printf("\n\n");
}
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else /* RECORDS ALREADY EXIST */
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->next->lname,temprecord->lname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->lname,temprecord->lname)>0)) /* RECORD BELONGS AT START OF LIST */
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
}
else /* RECORD IS SOMEWHERE IN MIDDLE OR END */
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
/* COPY ALL DATA FROM TEMP RECORD TO NEW ONE */
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
strcpy(tempptr->initial,temprecord->initial);
strcpy(tempptr->socialnum,temprecord->socialnum);
tempptr->balance = temprecord->balance;
}
int findssn(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *prev;
prev=NULL;
tempptr = *bankrecords;
if(tempptr!=NULL)
while((tempptr!=NULL) && (atoi(tempptr->socialnum)!=atoi(temprecord->socialnum))) {
prev=tempptr;
tempptr=tempptr->next;
}
if(tempptr==NULL)
{
if((temprecord->balance) > 0 )
{
printf("%s %s %s, %s, is being added to the list with a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
addtolist(bankrecords,temprecord);
printlist(bankrecords);
return 0;
}
else
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf the account does not exist.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
printlist(bankrecords);
return 0;
}
}
if((temprecord->balance) > 0 )
{
printf("%s %s %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if(((temprecord->balance) < 0 ) && ((fabs(temprecord->balance) < tempptr->balance)))
{
printf("%s %s %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if((temprecord->balance < tempptr->balance))
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
if(tempptr == *bankrecords) {
tempptr = tempptr->next;
free(tempptr);
printlist(bankrecords);
}
else {
prev->next=tempptr->next;
free (tempptr);
printlist(bankrecords);
}
return 0;
}
}
int main()
{
FILE *infile,*infile2;
CUSTOMER *bankrecords;
CUSTOMER *secondlist=NULL;
int test;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
/* READ IN FILE TO THE LIST */
while(!feof(infile))
{
fscanf(infile,"%s %s %s %s %lf",temprecord.lname,temprecord.fname,(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
}
infile2=fopen("data_2.dat","r");
if(infile2 == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
while(!feof(infile2))
{
fscanf(infile2,"%s %s %s %s %lf",temprecord.lname,temprecord.fname,(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
test=findssn(&bankrecords,&temprecord);
}
}
return 0;
}
-
Just throwing some code into a post with no message at all will not get me to help. I don't just grab people's random code and compile it. Furthermore, I do not just read random code, even if code tags are included.
I have better things to do with my time than to read your hundred or more lines of code to guess as to what your problem is. If you have a problem tell us what it is exactly and include comments telling us:
a) what input is taken
b) what output is given
c) what output is expected
Quzah.
-
I apologize for the non explanation. What my program is supposed to do is first read input from a file like such:
Springston Ray L 374926490 40392.37
Heineman Doug A 264592791 19473.08
Alagappan Solayappan Q 567493784 20493.76
Easton Dwight R 193465943 30284.33
Chatila Sami K 029375938 1943.88
Johnson Devin E 736549283 197493.93
Fazal Talha S 643847651 2947.08
Punjal Jagan D 463248598 29473.71
Pruitt Terry O 846239402 19374.50
Ma Glen T 264957640 134.29
Lewis Troy R 002947594 19374.12
Khan Eddy U 947604592 9476.66
Tran Michael B 204946731 652.09
Hopkins Weston S 463947204 2940.45
Store it in a linked list and then read data from another file similiar to this one:
Khan Eddy U 947604592 -80.50
Chatila Sami K 029375938 +2041.72
Ma Glen T 264957640 -409.85
Heineman Doug A 264592791 -503.96
Jiang Nan A 648937212 +29478.94
Tran Michael B 204946731 -705.08
Davis Brian W 294765949 -80.00
Hsieh Jill I 749394000 +8403.91
Sharma Rashmi O 994739403 +294.08
Chatila Sami K 029375938 -1029.84
Chatila Sami K 029375938 -4083.19
Nguyen Lam A 847308840 +4029.38
Alagappan Solayappan Q 567493784 -20493.75
Easton Dwight R 193465943 -10294.88
Alagappan Solayappan Q 567493784 -10.00
IT should read one record off teh second file one at a time and look to see if it already exists on the list if so it should do a series of checks like to see if its a withdrawal or a deposit and such and such. For the most part i have every thing working fine except the part where if the person exist on the list and the amount they are trying to withdraw is larger than their current balance i delete them. The only problem i have is when i read the last name Alagappan Solayappan, he is at the top of the list (the head) i need to delete the head and i can't figure out how to do it.
The code below is the part where im trying to delete the head.
Code:
if((temprecord->balance < tempptr->balance))
{
fprintf(infile3,"%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
if(tempptr == *bankrecords) {
}
else {
prev->next=tempptr->next;
free (tempptr);
printlist(bankrecords);
}
return 0;
}
-
All I can really say about linked lists, is to draw a picture of each record in the list and link them with lines, and go through your program changing what the tempptr or whatever is pointing at and then if you say wanna delete a record then work out what you'll be searching for and where all the pointers will be at once you find it and try to reconnect the list and delete the record without moving a pointer off of any of the records.
-
i drew boxes and i cannot get this last part to work for anything. The code i came up with to delete the head was this:
Code:
if(tempptr == *bankrecords) {
tempptr = tempptr->next;
free(tempptr);
printlist(bankrecords);
}
it did not work i got an error when i got the record that was the head and tried to free up the space.
-
ok ya on the right track, try:
Code:
if(tempptr == *bankrecords)
{
*bankrecords = (*bankrecords)->next;
free(tempptr);
printlist(bankrecords);
}
the reason is that bankrecords should always hold the first value in the list, otherwise your freeing the piece of memory that bankrecords is pointing too without telling bankrecords what to point to, so when you print the list again it will print nothing and the value of ->next will be screwed, etc. but in your example you were deleting the second record, not the first, the above will remove the first.
Note: if you want to use tempptr again straight after this code, you'll need to do:
Code:
tempptr = *bankrecords;
hope this helps, basically if you want to change the first value of your list always use *bankrecords, otherwise you can use tempptr.
-
Thank you so much, you have been of tremendous help. I know i have bothered you to death but thanks for helping
-
no problems, linked lists are always tricky because you always need to account for deleting records (nodes) in the middle and at the start and making sure that everything is pointing to something, etc.