Thread: Bank like system

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23

    Bank like system

    Hey,
    I am trying to make a system as a class assignment. I have only started but I am unsure of how to do one piece of code already. I have:
    Code:
    //Files using CSV Format
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    
    struct customer
    {
    	int accno;
    	char name[25];
    	char address[50];
    	
    };
    
    typedef struct customer C;
    
    void newperson( C * cArray);
    void printperson( C * cArray);
    
    #define CUSTOMERS "customers.txt"
    
    void main()
    {	
    	C  * cArray;
    	
    	int numPersons=0;
    
    	cArray = ( C * ) malloc( 1 * sizeof( C ) );
    	
    	system("COLOR 1E");
    	
    	newperson(cArray);
    	printperson(cArray);
    		
    	printf("\n\nWritten to file\n\n");
    }
    
    void newperson( C * cArray)
    {
    	char ch;
    	
    	FILE * customerfile = fopen(CUSTOMERS, "r");
    
    	if (customerfile==NULL)
    	exit(errno);
    	
    		printf("Enter name:\n");
    		gets( (cArray )->name);
    		
    		printf("Enter address:\n");
    		gets( (cArray )->address);
    		
    		printf("\nPress return to write information...\n");
    		ch = getchar();
    		fclose(customerfile);
    }
    
    void printperson( C * cArray)
    {
    
    		FILE * customerfile = fopen(CUSTOMERS, "a");
    	
    		if (customerfile==NULL)
    		exit(errno);
    		
    
    	printf("Name\t\tAddress");
    	
    	printf("\n%d", (cArray)->accno);
    	printf("\t%s", (cArray)->name);
    	printf("\t\t%s", (cArray)->address);
    
    	fprintf(customerfile,"%s,%s\n", cArray[0].name, cArray[0].address);
    	fclose(customerfile);
    }
    This much is working. What I want to be able to do is set up a counter so that accno will increment itself by 1 each time a new account is written to the file. Anyone any guidance on how to go about this?

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well first you will want to read how many accounts are in the file. A quick and easy way to do this could be to get the number of newlines then divide it by however many lines the data for each account uses.
    Last edited by mike_g; 02-18-2008 at 01:41 PM.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    If there is no one added yet how do i work around this? Should I just hardcode a sample customer to file to overcome this?

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    After opening the file test if the file pointer is null, if so the file does not exist so set the count to 0. Alternatively if a file exists but is empty then you count will finish at 0.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Agh, you are using void main I see. And gets too!
    Void main is undefined and gets is dangerous. Read:
    http://cpwiki.sourceforge.net/Gets
    and
    http://cpwiki.sourceforge.net/Void_main

    Try fixing those first.
    As for the count, well... you need to keep track of it. So you could write it at the beginning of your file.
    When the program runs, read back that number and increase it by one for every new account and write the new number to file.
    Last edited by Elysia; 02-19-2008 at 01:27 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    I'm trying a slightly diff approach now but keep getting an error early on. Help appreciated again
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    
    struct customer
    {
    	int accno;
    	char name[25];
    	char address[50];
    	
    };
    
    typedef struct customer C;
    
    void loaddata(C * cArray);
    void readperson( C * cArray);
    void printperson( C * cArray);
    
    #define CUSTOMERS "customer.txt"
    #define LINE_LENGTH 80					//Maximum buffer size for reading in file.
    
    int main(void)
    {
    	C  * cArray;
    	
    	int numPersons=0;
    	
    	cArray = ( C * ) malloc( 1 * sizeof( C ) );
    	loaddata(cArray);
    	readperson(cArray);
    	printperson(cArray);
    		
    	printf("\n\nWorking\n\n");
    }
    
    void readperson( C * cArray)
    {
    	char ch;	
    		
    		printf("Enter name:\n");
    		gets( (cArray )->name);
    		
    		printf("Enter address:\n");
    		gets( (cArray )->address);
    		
    		printf("\nPress any key to continue...\n");
    		ch = getchar();
    }
    
    void printperson( C * cArray)
    {
    
    		FILE * customerfile = fopen(CUSTOMERS, "a");
    	
    		if (customerfile==NULL)
    		exit(errno);
    		
    	printf("Name\t\tAge");
    	
    	printf("\n&#37;s", (cArray)->name);
    	printf("\t\t%s", (cArray)->address);
    	
    	fprintf(customerfile,"%s,%s\n", cArray[0].name, cArray[0].address);
    	fclose(customerfile);
    }
    
    void loaddata(C * cArray)
    {	
    	FILE * customerfile = fopen(CUSTOMERS, "r");
    	char tmpline[LINE_LENGTH];
    	
    	int linecount=0;									//Used as counter for number of lines
    	int k = 0;	
    	
    	if (customerfile==NULL)
    	exit(errno);
    	
    	    linecount = 0;
    	while (fgets(tmpline,LINE_LENGTH,customerfile) != NULL)
    		linecount++;
    	*cArray = malloc(linecount * sizeof(C));	
    	if (cArray==NULL)
    		exit(errno);
    
    	rewind(customerfile);
    
    	printf ("Data loaded\n");
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Were you not already corrected about gets?
    http://cpwiki.sourceforge.net/gets
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    I was but its working for time being so i want to try get something happening. can rectify gets then. Please help and i promise once i have time to learn new code i will never use gets again!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you also failed to mention what is wrong.
    Plus changing gets isn't difficult.
    fgets(buffer, sizeof(buffer), stdin)
    That's all.

    So
    gets( (cArray )->name);
    becomes
    fgets(cArray->name, sizeof(cArray->name), stdin);

    Code:
    *cArray = malloc(linecount * sizeof(C));
    This is incredibly wrong.
    You dereference the actual cArray pointer and store the address to the allocated memory there.
    Hmmm. How do you assign void* to customer?

    And after that... you aren't even reading the data from the file into the array.
    Yet later you're actually asking for new data and put it into your struct, making the whole previous loaddata useless.
    And you're just writing one entry at the end of the file, as well.
    What are you trying to accomplish?
    Last edited by Elysia; 02-27-2008 at 02:39 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    What i aim to do is that when i start the program it will read in the file of customers and see how many are there. when it has that done i want to add a customer +1 from the last customer but im getting stranded at it!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Add another customer you say... yet what are you going to do with all those? Why do you need to know how many customers there are?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    i will hopefully be puttin in a binary search to find customers so need it for that. i will also need to know for when the program is run several times how many customers are already there to increment the account number by 1 for the next customer.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You obviously need to dynamically allocate memory, so you should pass a pointer to pointer to your function to allocate the memory you need and then read the file into that array.
    There are two approaches to this. Either you can read through the entire file and count the lines and then fill the array or you can allocate n amount of structs, read n amount of structs and if it isn't enough, reallocate your array to hold more structs and continue reading.

    Once you have all of them in your big array, you can do whatever with it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    I want to do the first option where I read the lines.
    Is my code pure rubbish thus far so? The whole project I will want to have users who can have one of 3 types of account. They will be able to lodge or withdraw money from these account and get a statement. If I can get the accounts working including the account number i have a fair idea how to do the accounts. (I think)

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it just needs a little modification, that's all. I don't know if you think you can implement a double pointer system?
    The reason is that main also needs access to your dynamic buffer, so you must pass a pointer to that pointer and allocate memory in it.
    Give it a try.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 01:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM