Posted by Tammie on Thursday, April 19, 2012 at 8:22am.
Would you like to share with us what you have done so far, and what problems you are encountering, if any?
It is also not clear which language should be used to write this program.
Below is a small perl program which does the job. You may not know perl, but it should be possible to figure out what it's doing.
It does not validate input as a number, but it does illustrate some of the possible gotchas, and what can be done.
------------------------------------
print "Enter Account Number: ";
$acct = <STDIN>;
$bal = 500 + rand(500); #random balance from 500 to 1000
$bal = int($bal*100)*.01; #restrict to 2 decimal places
printf "Current Balance: \$%4.2f\n",$bal;
while (1) {
print "Deposit(D) or Withdrawal(W) or Quit(Q) ? ";
until (($dw = <STDIN>) =~ /^\s*[dw]\s*$/i) { # make sure onlt D or W is entered
exit if $dw =~ /^q/i;
chomp $dw;
print "\"$dw\" is not a valid choice.\n";
print "Deposit(D) or Withdrawal(W)? ";
}
$dw =~ s/\s+//g; # strip white space
$dw = "\U$dw"; # convert to upper case
if ($dw eq "D") {
$bal += update("Deposit");
} else {
$bal -= update("Withdrawal",$bal);
}
printf "New balance: \$%4.2f\n",$bal;
print "\nAnother transaction [Y]? ";
$yn = <STDIN>;
last unless $yn =~ /^y?\s*$/i; #exit unless Y or <cr> entered
}
# Get deposit/withdrawal amount.
# This function does not validate input as number
sub update {
my ($dw,$cb) = @_; # get D/W and current balance
print "Amount of $dw: \$";
$amt = <STDIN>;
return $amt if $dw eq "Deposit"; # allow any deposit
while ($amt > $cb) {
printf "You cannot withdraw \$%4.2f -- balance is only \$%4.2f
Please enter an amount less than \$%4.2f: \$",$amt,$cb,$cb;
$amt = <STDIN>;
}
return $amt;
}
Related Questions
programming and visual basic - These are two different types of questions, these...
C Programming - Write a program that prompts the user to enter a temperature, ...
Computer Programming - /* Develop a C program that will determine if a ...
programming MIPS ask - Write a MIPS assembly language program that will cover ...
Programming in c++ - Write a program in c++ that inputs a number [1-12] from the...
C programming - I'm suppose to write a program using functions and modules ...
Programming - Write a pseudocode program that asks the user for an integer ...
computer - Please read the following instructions (related project code) ...
computer - Create a new project Lab05e, and write a program that asks the user ...
Computer (Programming with Python) - Problem 1: Multiplication Drill Computers ...
For Further Reading