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