Original Post
I'm working through a book on C and I'm having trouble with one of the exercises. The program is supposed to simulate a bank account with deposits, withdrawals, and checks. When the user is done updating the account, it prints a bank statement with the updates.
I didn't think it would be too hard, but I'm getting weird values for everything. When I print out the values for the balance and everything else it looks like they haven't even been initialized and they definitely aren't what I expected (even with simple input).
What am I doing wrong?
Thanks
I didn't think it would be too hard, but I'm getting weird values for everything. When I print out the values for the balance and everything else it looks like they haven't even been initialized and they definitely aren't what I expected (even with simple input).
What am I doing wrong?
#include <stdio.h>struct Check{ int number; double amount;};void printStatement(double balance, double deposits[], int numDeposits, double withdrawals[], int numWithdrawals, struct Check checks[], int numChecks);int main(void){ double balance = 0.0; char input = ' '; double deposits[256]; double withdrawals[256]; struct Check checks[256]; int numDeposits = 0; int numWithdrawals = 0; int numChecks = 0; printf("XYZ Bank Statement Program\n"); printf("--- ---- --------- -------\n"); printf("\n"); printf("Enter beginning balance: "); scanf("\n%f", &balance); printf("\n"); printf("Enter transactions:\n"); while(input != 'q' && input != 'Q') { printf("(W)ithdrawal, (D)eposit, (C)heck, (Q)uit: ");; scanf("\n%c", &input); if (input == 'd' || input == 'D') { printf("Amount: "); scanf("%f", &deposits[numDeposits]); balance += deposits[numDeposits]; numDeposits++; } else if (input == 'W' || input == 'w') { printf("Amount: "); scanf("%f", &withdrawals[numWithdrawals]); balance -= withdrawals[numWithdrawals]; numWithdrawals++; } else if (input == 'c' || input == 'C') { printf("Check number: "); scanf("%d", &checks[numChecks].number); printf("Amount: "); scanf("%f", &checks[numChecks].amount); balance -= checks[numChecks].amount; numChecks++; } } printf("\n\n\n\n"); printf("Balance %.2f\n"); printf("%.2f\n", deposits[0]); printf("%.2f\n", withdrawals[0]); printf("%.2f\n",checks[0].amount); printf("\nThank You!\n\n"); printStatement(balance, deposits, numDeposits, withdrawals, numWithdrawals, checks, numChecks); return 0;}void printStatement(double balance, double deposits[], int numDeposits, double withdrawals[], int numWithdrawals, struct Check checks[], int numChecks){ int i; double totalDeposits = 0.0; double totalWithdrawals = 0.0; double totalChecks = 0.0; printf("B A N K S T A T E M E N T\n"); printf("- - - - - - - - - - - - -\n"); printf("Beginning balance:\t$%.2f\n\n", balance); printf("1 Deposit:\n"); for (i = 0; i < numDeposits; i++) { printf("$%.2f\n", deposits); totalDeposits += deposits; } printf("Total deposits: %.2f", totalDeposits); printf("2 Withdrawals:\n"); for (i = 0; i < numWithdrawals; i++) { printf("$%.2f\n", withdrawals); totalWithdrawals += withdrawals; } printf("Total withdrawals: %.2f", totalWithdrawals); printf("3 Checks:\n"); for (i = 0; i < numChecks; i++) { printf("%d\t$%.2f\n", checks.number, checks.amount); totalChecks += checks.amount; } printf("Total checks: %.2f", totalChecks);}Thanks