By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 449,167 Members | 1,190 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,167 IT Pros & Developers. It's quick & easy.

# help with program

 P: n/a hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write a C program that allows the user to make some simple banking transactions. The program should first prompt the user to enter the current balance of his/her bank account (in dollars and cents). The program should then prompt the user to enter the number of deposits to make, and then the number of withdrawals to make. Using a loop, the program should then prompt the user to enter the amount of the first deposit (to add to the bank account balance), the amount of the second deposit, the third, & etc., until the number of deposits have been processed.Using a second loop, the program should then prompt the user to enter the amount of the first withdrawal (to subtract from the bank account balance), the amount of the second withdrawal, the third, & , etc. until the number of withdrawals have been processed.Once all deposits and withdrawals have been made, the program should output the ending balance. The dialog with the user should look like: Welcome to the Sears Bank Balancing SystemEnter Enter current balance in dollars and cents: 256.40 Enter the number of deposits: 3 Enter the number of withdrawals: 2 Enter the amount of deposit #1: 10.50 Enter the amount of deposit #2: 12.25 Enter the amount of deposit #3: 125.30 Enter the amount of withdrawal #1: 120.35 Enter the amount of withdrawal #2: 35.60 *** The closing balance is \$248.50 *** At this point, the program should also output one of the following messages based on the closing balance. If the closing balance is greater than or equal to 5000.00, output: "*** Time to invest some money! ***" If the closing balance is between 2000.00 and 4999.99, output: "*** Maybe you should consider a CD. ***" Regarding error checking on user input, the following check should be made while users are entering the withdrawal amounts: If the withdrawal amount exceeds the current balance(including the new deposits), the program should issue the following error message: *** Withdrawal amount exceeds current balance. ***The program should then re-prompt for a lower withdrawal amount, so not to go below current balance amount. But, if the current balance goes to zero, no more withdrawals should be made and an appropriate message should appear. Here's what I have so far on my own: #include main () { /* Declaration of Variables */ int x; int num_dep; int num_with; float total_dep = 0; float initial_balance, running_bal; float withdrawal, total_with = 0; /* */ printf ("\nWelcome to the Milas Bank Balancing System \n"); /* Begin by asking the user to enter the amount of the deposit */ printf ("\nEnter current balance in dollars and cents: "); scanf("%f", &initial_balance); fflush(stdin); printf ("\nEnter the number of deposits: "); scanf("%f", &num_dep); fflush(stdin); printf ("\nEnter the number of withdrawals: "); scanf("%f", &num_with); fflush(stdin); running_bal = total_dep + initial_balance; total_dep = running_bal + initial_balance; /* Prompt user for Deposit Amounts */ for (x = 1; x <= total_dep; x++) if ( total_dep < 0 ) { printf ("*** Withdrawal amount exceeds current balance. ***\n"); x--; /* decrement counter to stay at same grade entry */ fflush(stdout); } else /* end if-else */ } return 0; } // end main Nov 13 '05 #1
Share this Question
2 Replies

 P: n/a atreide_son wrote: The dialog with the user should look like: Welcome to the Sears Bank Balancing SystemEnter Enter current balance in dollars and cents: 256.40 Enter the number of deposits: 3 Enter the number of withdrawals: 2 So your design will look something like this: main GetBalance GetDepositCount GetWithdrawalCount for each deposit GetDepositAmount UpdateBalance while balance > 0 && more withdrawals to do GetValidWithdrawalAmount UpdateBalance DisplayClosingBalance DisplayBalanceDependentMessage Here's what I have so far on my own: #include main () Better make that int main(void) { /* Declaration of Variables */ int x; int num_dep; int num_with; float total_dep = 0; float initial_balance, running_bal; float withdrawal, total_with = 0; /* */ printf ("\nWelcome to the Milas Bank Balancing System \n"); /* Begin by asking the user to enter the amount of the deposit */ printf ("\nEnter current balance in dollars and cents: "); scanf("%f", &initial_balance); fflush(stdin); Undefined behaviour. fflush's behaviour is defined only for streams open for output or update. I'll have a more detailed look at your code, and perhaps give some more hints for its solution, if I get time later on today. -- Richard Heathfield : bi****@eton.powernet.co.uk "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton Nov 13 '05 #2

 P: n/a Without hesitation, atreide_son asserted (on or about 07/27/03 23:48) that: hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write a C program that allows the user to make some simple banking transactions. The program should first prompt the user to enter the current balance of his/her bank account (in dollars and cents). The program should then prompt the user to enter the number of deposits to make, and then the number of withdrawals to make. Using a loop, the program should then prompt the user to enter the amount of the first deposit (to add to the bank account balance), the amount of the second deposit, the third, & etc., until the number of deposits have been processed.Using a second loop, the program should then prompt the user to enter the amount of the first withdrawal (to subtract from the bank account balance), the amount of the second withdrawal, the third, & , etc. until the number of withdrawals have been processed.Once all deposits and withdrawals have been made, the program should output the ending balance. The dialog with the user should look like: Welcome to the Sears Bank Balancing SystemEnter Enter current balance in dollars and cents: 256.40 Enter the number of deposits: 3 Enter the number of withdrawals: 2 Enter the amount of deposit #1: 10.50 Enter the amount of deposit #2: 12.25 Enter the amount of deposit #3: 125.30 Enter the amount of withdrawal #1: 120.35 Enter the amount of withdrawal #2: 35.60 *** The closing balance is \$248.50 *** At this point, the program should also output one of the following messages based on the closing balance. If the closing balance is greater than or equal to 5000.00, output: "*** Time to invest some money! ***" If the closing balance is between 2000.00 and 4999.99, output: "*** Maybe you should consider a CD. ***" Regarding error checking on user input, the following check should be made while users are entering the withdrawal amounts: If the withdrawal amount exceeds the current balance(including the new deposits), the program should issue the following error message: *** Withdrawal amount exceeds current balance. ***The program should then re-prompt for a lower withdrawal amount, so not to go below current balance amount. But, if the current balance goes to zero, no more withdrawals should be made and an appropriate message should appear. Here's what I have so far on my own: #include main () { /* Declaration of Variables */ int x; int num_dep; int num_with; float total_dep = 0; float initial_balance, running_bal; float withdrawal, total_with = 0; /* */ printf ("\nWelcome to the Milas Bank Balancing System \n"); /* Begin by asking the user to enter the amount of the deposit */ printf ("\nEnter current balance in dollars and cents: "); scanf("%f", &initial_balance); fflush(stdin); Sorry, but no. The operation of fflush() is undefined when given stdin as it's parameter. This is not the way to eliminate unread input. FWIW, you /should/ check the return value from the scanf(). It tells you how many data items were matched, and can save you from errors when your customer enters One Hundred Thirty Dollars Seventy Three Cents to your "Enter current balance" prompt. printf ("\nEnter the number of deposits: "); scanf("%f", &num_dep); The scanf "%f" sequence will look for and capture a floatingpoint number, and store it in a floatingpoint variable who's address you provide. You didn't provide the address of a floatingpoint variable; num_dep is defined as an integer. This is going to cause you /lots/ of problems later on. fflush(stdin); Same points as with the previous scanf() and fflush(stdin) sequence. printf ("\nEnter the number of withdrawals: "); scanf("%f", &num_with); fflush(stdin); Same three points as above. You really need to understand scanf() and fflush(). running_bal = total_dep + initial_balance; total_dep is initialized to 0, and is not changed by your initial logic. This is a long way to say running_bal = initial_balance; total_dep = running_bal + initial_balance; Wow. I just doubled my money, and I haven't done any transactions yet. At this point, running_bal is equal to initial_balance so, your logic makes total_dep equal to initial_balance + initial_balance To me, that makes total_dep /twice/ initial_balance Perhaps you meant to code this as total_dep = running_bal - initial_balance; /* Prompt user for Deposit Amounts */ for (x = 1; x <= total_dep; x++) Huh?? You're looping from 1 to twice his initial balance? This makes no sense. You probably intended to loop so as to execute the number of deposits the user wants to make (one loop iteration per deposit). If so, then you really should be looping until x exceeds num_dep if ( total_dep < 0 ) { printf ("*** Withdrawal amount exceeds curren balance. ***\n"); We're making deposits. While I understand that the user might enter a negative number as a deposit amount, you really should a) disallow that in your edit, and b) change the wording on this printf(). x--; /* decrement counter to stay at same grade entry */ fflush(stdout); } else else what? /* end if-else */ } return 0; } // end main A couple of hints: 1) deposits and withdrawals are two sides of the same coin. A deposit of -10.00 is a withdrawal of 10.00. Perhaps you can simplify your logic a bit by using this observation to your advantage. 2) fflush() works only on NULL or output files. It doesn't work on input files. You need to find some other way to discard the user's input (if that is what you need to do). A properly designed program usually doesn't have to discard input in this manner, though. 3) Become familiar with the scanf() and printf() escape sequences. 4) Rather than coding your first attempt, try to /explain/ it. It helps (at least it helps me, and I've been programming for over 25 years, in a Banking environment at that). For instance, your program might be explained as get the user's initial balance, set current_balance from initial balance get a count of the number of transactions for each transaction counted from above get the transaction_type (debit or credit) get the transaction_amount if transaction_type is "debit" if current_balance - transaction_amount < 0.0 say "Transaction declined - Overdraft" else current_balance = current_balance - transaction_amount end-if else current_balance = current_balance + transaction_amount end-if end_for print current_balance Notice that there are things missing. Obviously, a first cut can't cover all conditions, but it should at least give you an idea of what the conditions are. In this case, I have'nt accounted for the user entering a -ve transaction count, or an invalid transaction_type, or even a -ve transaction_amount. That's what goes into the next iteration of the explanation. Once it gets detailed enough to see what sort of code is necessary, you can start coding. For experienced programmers, that may be almost immediately, but newbies will probably need to go through several iterations of "explaining the code" before they are in a position to actually code the program. -- Lew Pitcher Master Codewright and JOAT-in-training Registered Linux User #112576 (http://counter.li.org/) Slackware - Because I know what I'm doing. Nov 13 '05 #3

### This discussion thread is closed

Replies have been disabled for this discussion.