-
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.