Small error in my program C Language -
so program not outputting proper value numdeposit or numcheck, outputs value of 0 both of these. think prime area of interest main function , bottom function can't see 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 not opened input."); fprintf(csis, "account.txt not opened input."); exit(1); } if (!(csis = fopen("csis.txt", "w"))) { printf("csis.txt not opened output."); fprintf(csis, "csis.txt not opened 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 operator precedence issue.
*numdeposit++;
the postfix ++ has higher precedence *. it's evaluating to
*(numdeposit++);
which nothing (it increments pointer references value @ memory location). can do
(*numdeposit)++;
or
*numdeposit += 1;
see http://en.cppreference.com/w/cpp/language/operator_precedence.
Comments
Post a Comment