Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with C

Started by Dasil Nov 20, 2010 at 9:21 PM 3 replies 1k views
Original Post
Dasil
Dasil
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?

#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
Zahlman
Zahlman
Quote:
Original post by Dasil
printf("Balance %.2f\n");


Care to provide a value to fill in the '%.2f' with? :) In C, you can generally expect everything to break down completely as soon as there's a single mismatch of that sort. You might be able to get your compiler to warn you about these things in simple cases (although it can't in general, because the format string doesn't have to be a literal).
Dasil
Dasil
Oops. That part was just a test, and it's been removed now. The program still doesn't work though. It says that almost everything is 92559604889553262000000000000000000000000000000000000000000000000.00, and a few are the same but negative.

Thanks for the reply though.
Buckeye
Buckeye
double balance;scanf("%f",&balance); // reads in and converts to 4-byte float, stored in a double memory area

You're scanning float values into double variables.
Change everything to float, or input to float variables and convert to doubles for storage.

&balance is just an address to the scanf function. It doesn't care where you store anything or what the type is that the address points to.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
Dasil
Dasil
Thanks. I changed the conversion specification or whatever it's called and it works now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.