Thread: Updating Elements with Arrays

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Updating Elements with Arrays

    NOTE: Title of thread is Incorrect I meant Updating Elements in Arrays!!!

    For some apparent reason I finished typing my code. No errors it seems like I'm missing something here. I'm trying to update my accounts but the balance keeps coming out to zero. Don't show me the answer but guide me on a path to correcting this problem.

    Code:
     #include <stdio.h>/* Header */
    
    
    #define MAX_ACCTS 10
    int accounts[MAX_ACCTS] =
            {20181, 20467, 20813,    21354,  21358,  21359,    22027, 22041};
    double balances[MAX_ACCTS] =
            {0,    145.72, 10133.00, -17.44, 612.98, 1415.30, 313.21, 3131.32};
    
    
    int findAccount(int accountID) {
        int index = -1;
        
        index = 1; 
        return(index);
    }
    
    
    void printAccountBal(int accountIndex) {
        int i = 0.0;
        printf("The Account Number is %d. Current Balance is %f\n\n", accounts[i], balances[i]);
    }
    
    
    
    
    
    
    void deposit(int accountIndex) {
        float amount;
        int i = 0;
        printf("Enter Amount: " );
        scanf("%f", &amount);
            for (i=0; i<MAX_ACCTS; ++i)  
    
    
            do {
                if (amount <= 0)
                    printf("Unacceptable Value. Please Insert Positive Number");
                
                
                }
    
    
            while ((i < accountIndex) && (++i < MAX_ACCTS));
                
                balances[i] += amount;
    }
    
    
    void withdraw(int accountIndex) {
        float amount;
    
    
        int i = 0.0;
        printf("Enter Amount: " );
        scanf("%f", &amount);
            for (i=0; i<MAX_ACCTS; ++i)  
    
    
            do {
                if (amount <= 0)
                    printf("Unacceptable Value. Please Insert Positive Number");
                
                
                }
    
    
            while ((i < accounts [7]) && (++i < balances[7]));
                
                balances[i] -= amount, ++i;
            
    }
            
        
        
    
    
    
    
    int main(void) {
        int choice;
        int accountID, accountIndex;
        int accountIDgood;
    
    
    
    
        printf("Bank account simulator\n");
    
    
        do {
            accountIDgood = 0;    /* False */
            do {
                printf("\nEnter account ID: ");
                scanf("%d", &accountID);
                if (accountID < 20000 || accountID > 29999) {
                    printf("Account ID is not correct format.\n");
                } else {
                    accountIndex = findAccount(accountID);
                    if (accountIndex == -1) {
                        printf("Account ID %d is not valid.\n",
                              accountID);
                    } else {
                        accountIDgood = 1;    /* True */
                    }
                }
            } while (!accountIDgood);
    
    
            printAccountBal(accountIndex);
    
    
            do {
                printf("Choose from the following menu:\n");
                printf("\t0 - Exit the program\n");
                printf("\t1 - Change accounts\n");
                printf("\t2 - Make deposit\n");
                printf("\t3 - Make withdrawal\n");
                printf("Enter your choice: ");
                scanf("%d", &choice);
                printf("\n");
                
                switch (choice) {
                    case 2:
                        deposit(accountIndex);
                        printAccountBal(accountIndex);
                        break;
                    case 3:
                        withdraw(accountIndex);
                        printAccountBal(accountIndex);
                        break;
                    default:
                        if (choice != 0 && choice != 1) {
                             printf("Your choice (%d) is not valid.\n",
                                 choice);
                        }
                        break;
                }
            } while (choice != 0 && choice != 1);
        } while (choice != 0);
    
    
        return(0);
    }
    Last edited by gdirty; 11-13-2012 at 12:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping elements in Arrays
    By november1992 in forum C Programming
    Replies: 11
    Last Post: 03-10-2012, 01:58 AM
  2. Comparing Elements of 2 arrays
    By SupraMan in forum C Programming
    Replies: 28
    Last Post: 11-23-2010, 02:43 PM
  3. Replies: 12
    Last Post: 03-11-2010, 02:28 PM
  4. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  5. calculating elements of arrays
    By Kristin in forum C Programming
    Replies: 2
    Last Post: 11-02-2003, 10:38 AM