Connecting Tech Pros Worldwide Forums | Help | Site Map

program help (checkbook)

dan
Guest
 
Posts: n/a
#1: Jul 19 '05
can someone tell me how to have balance add the initial 10 dollars
just one time and not every time

thanks

int main()
{
double initial = 10, transaction;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision (2); // for 2 decimal place, because that is what
money uses

cout << "************Enter 0.00 to quit this
program************\n\n";
cout << "Initial balance: " << initial;
cout << endl;

double balance = 0;
double penalty = 0;
double invalid = 0;
do
{
cout << "\nEnter transaction amount: ";
cin >> transaction;
balance = balance + transaction + initial;
invalid = balance - transaction - initial;
if (balance >= 0)
{
cout << "Balance: " << balance;
cout << endl;
}
if ((balance < 0 && balance > -100))
{
cout << "Overdraw fee: $10.00\n";
penalty = balance - initial - 10;
cout << "Balance: " << penalty;
cout << endl;
}
if (balance < -100)
{

cout << "Insufficient funds to complete transaction.\n";
cout << "Balance: " << invalid;
cout << endl;
}
} while (transaction != 0.00); //sentinel value
cout << endl;
cout << "End of day balance: " << balance;

cout << endl << endl;
system ("pause");
return EXIT_SUCCESS;
}

David Rubin
Guest
 
Posts: n/a
#2: Jul 19 '05

re: program help (checkbook)


dan wrote:[color=blue]
>
> can someone tell me how to have balance add the initial 10 dollars
> just one time and not every time
>
> thanks
>
> int main()
> {
> double initial = 10, transaction;[/color]
[snip][color=blue]
> double balance = 0;[/color]

I think the intent is for the program to start with a balance of 10, so
replace the above lines with

double balance = 10.0;

Then...
[color=blue]
> do
> {
> cout << "\nEnter transaction amount: ";
> cin >> transaction;
> balance = balance + transaction + initial;[/color]

balance += transaction;

[snip][color=blue]
> if ((balance < 0 && balance > -100))
> {
> cout << "Overdraw fee: $10.00\n";[/color]
[snip]
balance -= 10;

[snip][color=blue]
> }
> if (balance < -100)
> {
>
> cout << "Insufficient funds to complete transaction.\n";[/color]
[snip][color=blue]
> }[/color]

cout << "Balance: " << balance << endl;
[color=blue]
> } while (transaction != 0.00); //sentinel value
> cout << endl;
> cout << "End of day balance: " << balance;
>
> cout << endl << endl;
> system ("pause");
> return EXIT_SUCCESS;
> }[/color]

BTW, using "0.00" to terminate input is not particularly user-friendly.
A more robust solution is to allow for alphanumeric input, and process,
e.g., 'q' to quit, and numbers as valid transactions. This requires only
a slightly more complex input method than you've got.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Kevin Goodsell
Guest
 
Posts: n/a
#3: Jul 19 '05

re: program help (checkbook)


dan wrote:
[color=blue]
> can someone tell me how to have balance add the initial 10 dollars
> just one time and not every time[/color]

I suggest posting a minimum complete program that demonstrates the
problem you are having. Extra, unrelated code just obscures the main
issue and lessens your chance of getting an accurate response. The FAQ
has recommendations for how to post code:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Closed Thread