Thread: Allocating input to an array

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Allocating input to an array

    I'm sorry if this is a stupid question, but I have only just started my first programming language and I'm stumped on where to go now.

    I'm trying to write a program that will take input from a file that will do some basic banking. The input will be in the format of "Transaction type (DB or CR), Acct #, Transaction Amt".

    So far, I figure I'm supposed to make an array of two parts for the transaction. 1000000, which is the maximum number of transactions, and 3, so that it takes in all three parts of the input for each transaction.

    The maximum number of accounts is 25, so I'm making a separate array with 2 parts to take care of that. It is 25, for the number of accounts, and 2, for the account number and balance.

    So, my questions are...

    1) How can I take the input received from the first array and put it into the second array?

    2) How would I read an account number, create that account number in the array, then next time it sees that number, it does not change the account number, but will adjust the balance.

    3) I think I have an idea of how to adjust the transaction depending on DB or CR, but I want to verify. Would I have to use an "if then" statement that says if DB then balance = balance + x? and if CR then balance = balance - x?

    4) How do I put limits on the input that would reject the input should it not follow the proper requirements? i.e. instead of putting DB or CR the user inputs 34? I'd like it to reject that input and restart the question.


    I understand if I'm asking too much here, and I do not expect you guys to write the code for me, but could you point me in the right direction? I can't seem to find anything in 2 of my books relating to this and I've been looking all over the web. Of course, I might not understand it when I first see it as this is all greek to me lol.

    Here is my code so far if you want to look it over:

    Code:
    /* Lab01.cpp : Defines the entry point for the console application.
    */
    
    #include <stdio.h>
    #include <string.h>
    
    #define MAXACNTS 1000001
    
    int account[25][2];
    int trans[1000000][3];
    int i = 0; /* Loop Counter */
    
    
    int main(void)
    {
    
    	for (i = 0; i < MAXACNTS; i++)
    	{
    		printf("Press CTRL+Z to quit\n");
    		printf("Enter Transaction Type:\n");
    		scanf("%s\n", &trans[i][0]);
    		printf("Enter Account Number:\n");
    		scanf("%d\n", &trans[i][1]);
    		printf("Enter Transaction Amount:\n");
    		scanf("%d\n", &trans[i][2]);
    	}
    	for (i = 0; i < MAXACNTS; i++)
    	{
    		
    	}
    	for (i = 0; i < MAXACNTS; i++)
    	{
    		printf("%d %d", account[25][0], account[25][1]);
    	}
    
    	return 0;
    }


    Thanks for any and all help.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't believe you need an array for the transactions, just for the accouints. Each account will have 3 parts:

    1) The account number (used for searching and must be unique)
    2) The account owner
    3) The current balance

    I would advise making the above, into a text file. Then when the program starts, it can build it's array from that file, without you having to enter the data for all accounts, each time you want to run the program.

    Saves a lot of time and keyboarding!

    The above screams for a struct. The struct definition goes before main():

    (This is an idea, not runnable code)
    Code:
    struct accouint {
      unsigned long number;
      char lname[25];
      char fname[25];
      unsigned long dollar;
      int cents;
    }
    
    struct account acc[50] //make an array of 50 accouts
    Now you have a second text file, with transactions in it:
    Account number Debit or Credit Amount
    1001 D 56.23
    1185 C 300.00

    etc.

    Code:
    while((fscanf(fp,"%U %s %s %U %c %d", acc.number, acc.lname acc.fname, acc.dollar, dot, acc.cents) == 6) {
    
    //dot is just for the decimal place
    
    }
    Your program first loads the accounts data, into the array of structs, and then opens the transaction data file, and begins processing in a loop similar to the above while loop.

    The accounts will all be sorted according to the account number, and you'll want a binary search to find them (hardly necessary with these numbers, but great when your banking business has 10,000 customers.

    Dividing up the dollars and the cents makes good sense, because it stops any floating point error, before it can even start.

    Debit and Credit refer to bookkeeping. Debits are deposits, credits are withdrawals.

    When all the transactions are processed, you need to write out the new data from the array to the ledger file, again using a do while or while loop.

    For a more robust program, I'd say put the array declaration in main(), and dynamically malloc the memory, but that would just complicate matters unnecessarily for a beginner.
    This is something you can always add later on.

    Oooo-rah! I smell version 2!

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    Thanks for the info!

    I can see that you're used to implementing things like this in a much higher scale environment than me, but I'm just taking my first programming course in college lol. I haven't learned about structures yet, but I'm googling the information to learn how to bette r implement it into my situation.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. entering numbers to array in a row whithout length input
    By transgalactic2 in forum C Programming
    Replies: 55
    Last Post: 01-02-2009, 04:02 AM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM