473,766 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(includi ng 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_balanc e);
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 4679
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
GetWithdrawalCo unt
for each deposit
GetDepositAmoun t
UpdateBalance
while balance > 0 && more withdrawals to do
GetValidWithdra walAmount
UpdateBalance
DisplayClosingB alance
DisplayBalanceD ependentMessage

<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_balanc e);
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.pow ernet.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(includi ng 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_balanc e);
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_typ e (debit or credit)
get the transaction_amo unt
if transaction_typ e is "debit"
if current_balance - transaction_amo unt < 0.0
say "Transactio n declined - Overdraft"
else
current_balance = current_balance - transaction_amo unt
end-if
else
current_balance = current_balance + transaction_amo unt
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_typ e, or even a -ve
transaction_amo unt. 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
2757
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 storing products in
6
4347
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 result in any way. Who can help me, I thank you very very much. list.cpp(main program) //-------------------------------------------------------------------------- - #pragma hdrstop #pragma argsused
5
3011
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 homework for me.. I am merely asking for help, perhaps pointers as to where to begin.. I've never used access before.. I'm rather cluey when it comes to
7
3294
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 I believe if it's solved, the remaining stuff is easy... my full program until now is here: http://www.geocities.com/tom4_h4wk/progmail.zip the problem is the segmentation fault when main trys to run leficheiro.c.... the *.c2 files are the...
2
2123
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 my other includes then I get lots of errors like C2011: 'fd_set' : 'struct' type redefinition warning C4005: 'FD_CLR' : macro redefinition which I understand are due to the fact that windows.h is being included in another header file as well as...
2
2145
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 need help, could someone do, anything to help me i don't have much time, here are the instructions: Instructions: In this project, you are asked to develop your own spell-checker utility. We make suggestions to help get you started. You should...
6
1642
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 encountered a problem and needs to close. We are sorry for the inconvience. If you were in the middle of something, the information you were working on might be lost
1
1931
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 http://oldmill.uchicago.edu/~wilder/Code/random/. Firstly, I got the notorious "fatal error C1010: unexpected end of file while looking for precompiled header directive" error when I tried to compile 'randtest.c' provided by wilder.
1
1929
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 http://oldmill.uchicago.edu/~wilder/Code/random/. Firstly, I got the notorious "fatal error C1010: unexpected end of file while looking for precompiled header directive" error when I tried to compile 'randtest.c' provided by wilder.
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.