程序C语言中的小错误

时间:2014-03-20 03:08:31

标签: c

因此该程序没有为numDeposit或numCheck输出正确的值,它为这两个输出值为零。我认为感兴趣的主要领域是主要功能和底层功能,但我无法看到我做错了什么。

#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);
}

1 个答案:

答案 0 :(得分:2)

这是运营商优先问题。

*numDeposit++;

postfix ++的优先级高于*。所以它正在评估

*(numDeposit++);

这基本上什么都不做(它增加指针然后引用该内存位置的值)。你可以做到

(*numDeposit)++;

*numDeposit += 1;

请参阅http://en.cppreference.com/w/cpp/language/operator_precedence