473,324 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

help with program

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 <stdio.h>
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
2 4652
atreide_son wrote:

<snip>
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

<snip>

Here's what I have so far on my own:

#include <stdio.h>
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.

<snip>

--
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
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 <stdio.h>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
2
by: Bsnpr8 | last post by:
I need help guys, i have to many stuff to do, because i am in my last 2 weeks of the university, my last assignment is to do a spell checker in C++, i have an idea but nothing is coming out. I really...
6
by: HelpME | last post by:
I wrote a program in Vb.Net that was running fine. However I am unable to install it on a couple of machines. When i run it I get a windows error message that says My Project.exe has...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.