简体   繁体   中英

Small error in my program C Language

So this program is not outputting the proper value for numDeposit or numCheck, it outputs a value of zero for both of these. I think the prime area of interest is the main function and the bottom function but I can't see what I did wrong.

#include <stdio.h>
#include <stdlib.h>

FILE *csis;

FILE *fp;

void outputHeaders();

void initialBalance(double amount, double *balance, double *service, double *openBalance);

void deposit(double amount, double *balance, double *service, int *numDeposit, double *amtDeposit);

void check(double amount, double *balance, double *service, int *numCheck, double *amtCheck);

void outputSummary(int numDeposit, double amtDeposit, int numCheck, double amtCheck, double openBalance, double service, double closeBalance);


int main(void) {

    char code;
    double amount, service, balance;
    double amtCheck, amtDeposit, openBalance, closeBalance;
    int numCheck, numDeposit;

    if (!(fp = fopen("account.txt", "r"))) {
        printf("account.txt could not be opened for input.");
        fprintf(csis, "account.txt could not be opened for input.");
        exit(1);
    }
    if (!(csis = fopen("csis.txt", "w"))) {
        printf("csis.txt could not be opened for output.");
        fprintf(csis, "csis.txt could not be opened for output.");
            exit(1);
    }

    amount = 0.0;
    service = 0.0;
    balance = 0.0;
    amtCheck = 0.0;
    amtDeposit = 0.0;
    openBalance = 0.0;
    closeBalance = 0.0;
    numCheck = 0;
    numDeposit = 0;

    outputHeaders();

    while (!feof(fp)) {
        fscanf(fp, "%c %lf\n", &code, &amount);
        if (code == 'I'){
            initialBalance(amount, &balance, &service, &openBalance);
        }
        else if (code == 'D') {
            deposit(amount, &balance, &service, &numDeposit, &amtDeposit);
        }
        else {
            check(amount, &balance, &service, &numCheck, &amtCheck);
        }
    }

    closeBalance = balance - service;
    outputSummary(numDeposit, amtDeposit, numCheck, amtCheck, openBalance, service, closeBalance);
    getch();
    fclose(csis);
    fclose(fp);
    return 0;
}

void outputHeaders(){

    printf("Transaction\tDeposit\tCheck\tBalance\n");

    printf("-----------\t-------\t-----\t-------\n");

}

void initialBalance(double amount, double *balance, double *service, double *openBalance){

    *service += 3;
    *balance += amount;
    *openBalance = amount;
    printf("Initial Balance\t\t\t%.2f\n", amount);
}

void deposit(double amount, double *balance, double *service, int *numDeposit, double *amtDeposit){

    *balance += amount;
    *service += .03;
    *numDeposit++;
    *amtDeposit += amount;
    printf("Deposit\t\t%.2f\t\t%.2f\n", amount, *balance);
}

void check(double amount, double *balance, double *service, int *numCheck, double *amtCheck){

    *balance -= amount;
    *service += .06;
    *numCheck++;
    *amtCheck += amount;
    if (*balance < 0){
        *service += 5;
    }
    printf("Check\t\t\t%.2f\t%.2f\n", amount, *balance);
}

void outputSummary(int numDeposit, double amtDeposit, int numCheck, double amtCheck, double openBalance, double service, double closeBalance){

    printf("\nSummary\n---------------------------------------\n");
    printf("Opening Balance: %.2f\n", openBalance);
    printf("Number of Deposits: %d \tAmount Deposited: %.2f\n", numDeposit, amtDeposit);
    printf("Number of Checks: %d \tAmount Checked: %.2f\n", numCheck, amtCheck);
    printf("Service Charges: %.2f\n", service);
    printf("Closing Balance: %.2f\n", closeBalance);
}

It's an operator precedence issue.

*numDeposit++;

The postfix ++ has a higher precedence than *. So it's evaluating to

*(numDeposit++);

Which basically does nothing (it increments the pointer then references the value at that memory location). You can do

(*numDeposit)++;

Or

*numDeposit += 1;

See http://en.cppreference.com/w/cpp/language/operator_precedence .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM