Sorry for the 2nd post but I thought it might be seen better than if it was in my other post, which btw, thanks everyone in there for helping me.


The second program i have to write is about the user inputing their annual interest rate, current balance, and amount of months since they began the account. Then using a loop have them input the amount withdrawn and deposited for each month and at the end output the total balance, number of withdrawals, number of deposits, and total interest earned.


We are supposed to stop them from inputing negative numbers for deposits and withdrawals. My variables are doubles and i forgot that you can't make a double unsigned like you can a char or int.

My question is how do I stop them from inputing a negative number if the variable is a double?


This is the code so far:

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
	double rate;
	double balance;
	int months;
	double deposit;
	double withdrawn;
	int tdeposit = 0;
	int twithdrawn = 0;
	double tinterest = 0;
	
	cout << "Please input the annual interest rate [i.e. 1.5% is .015]" << endl;
	cout << "Then input the starting balance." << endl;
	cout << "Then input the number of months passed since the account was established." << endl;
	cin >> rate >> balance >> months;

		for(int i = 1; i <= months; i++)
		{
			cout << "Enter the amount deposited for the month." << endl;
			cin >> deposit;
			balance = balance + deposit;
			if (deposit > 0)
			{
				tdeposit = tdeposit + 1;
			}

			cout << "Enter the amount withdrawn for the month." << endl;
			cin >> withdrawn;
			if (withdrawn > 0)
			{
				twithdrawn = twithdrawn + 1;
			}

			balance = balance - withdrawn;
			if (balance < 0)
			{
				cout << "Your balance has gone below $0. Your account has been termintated" << endl;
				break;
			}
			double interest = rate / 12;
			tinterest = tinterest + (interest * balance);
			balance = (interest * balance) + balance;
			cout << "Your balance is $ " << fixed << setprecision(2) << balance << endl;
		}
		cout << "The ending balance is $ " << balance << endl;
		cout << "The total number of deposists are " << tdeposit << endl;
		cout << "The total number of withdrawals are " << twithdrawn << endl;
		cout << "The total interest earned is " << tinterest << endl;

}

Recommended Answers

All 3 Replies

I'd do something like this:

do
{
     cin >> deposit;
}
while (deposit < 0);

You may want to put some error message in there explaining what the user did wrong. But this method will keep prompting the user until he/she gives legal input.

Maybe it's just where I put it in the code but after doing that it just moves on to asking the user to input the amount withdrawn and doesn't prompt the user to input a deposit again, and messes up the deposit tally.

EDIT: NVM i got it to work

If you just want the value to always be positive than you can do:

if(deposit < 0)
    deposit *= -1;
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.