ok, so I just started looping today and the simpler programs are all right, but we got a pretty complex assignment which I am pretty lost on. here's the assignment:

Write a program that updates a person’s bank balance based on withdrawals and deposits.

Ask the user to enter the initial value in the account.
In a FOR loop:
• Prompt the user
• Deposits are positive values
• Withdrawals are negative values.
• Enter 0 to terminate the loop.
• Add or subtract the correct amount from the balance and display the balance
• Do not allow withdrawals of amounts larger than the balance in the account. (Use a continue if this is tried)

And here's what I have so far:

#include<iostream>
using namespace::std;

int main()
{
double balance,d;
char h;
int x;
cout<<"Enter your total balance\n";
cin>>balance;
for(x=0;x<=5;x++)
{
cout<<"Enter the amount you wish to deposit or withdraw. Include a negative(-) sign before amount for deposits.\n";
cin>>d;
if(balance>0)
cout<<"Your total balance after deposit is $"<<balance+d<<endl;
if(balance<0)
cout<<"Your total after withdrawal balance is $"<<balance-d<<endl;
if(d>balance)
continue;

}
cin>>x;
return 0;
}

Obviously, my code has some flaws and i still haven't figured out how I would go about outputting the balance and configuring the code to recognize a negative number as a withdrawal and a positive one as a deposit. Same goes for terminating the program by entering 0.

Any C++ aficionados, engineers, or whatever who can help me with this? thanks in advance.

ok, so I just started looping today and the simpler programs are all right, but we got a pretty complex assignment which I am pretty lost on. here's the assignment:

Write a program that updates a person’s bank balance based on withdrawals and deposits.

Ask the user to enter the initial value in the account.
In a FOR loop:
• Prompt the user
• Deposits are positive values
• Withdrawals are negative values.
• Enter 0 to terminate the loop.
• Add or subtract the correct amount from the balance and display the balance
• Do not allow withdrawals of amounts larger than the balance in the account. (Use a continue if this is tried)

And here's what I have so far:


#include<iostream>
using namespace::std;

int main()
{
double balance,d;
char h;
int x;
cout<<"Enter your total balance\n";
cin>>balance;
for(x=0;x<=5;x++)
{
cout<<"Enter the amount you wish to deposit or withdraw. Include a negative(-) sign before amount for deposits.\n";
cin>>d;
if(balance>0)
cout<<"Your total balance after deposit is $"<<balance+d<<endl;
if(balance<0)
cout<<"Your total after withdrawal balance is $"<<balance-d<<endl;
if(d>balance)
continue;

}
cin>>x;
return 0;
}

Obviously, my code has some flaws and i still haven't figured out how I would go about outputting the balance and configuring the code to recognize a negative number as a withdrawal and a positive one as a deposit. Same goes for terminating the program by entering 0.

Any C++ aficionados, engineers, or whatever who can help me with this? thanks in advance.

Code tags and formatting please:

[code]

// paste code here

[/code]

For one, I assume this is backwards since you are adding?

Include a negative(-) sign before amount for deposits.

Should be negative for withdrawals, positive for deposits. Either that or you need to subtract rather than add the user's input.

Second, you need to change the balance variable's value, not just display:

cout<<"Your total balance after deposit is $"<<balance+d<<endl;

So do this instead:

balance = balance + d;

Then display balance rather than balance + d .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.