need help! need to prevent withdraw function from occuring when savings balance is below 25. The program will run, although when the balance gets below $25 it just states "account inactive" which is fine but then the menu does not display again. This is what i have so far:

int main()
{
    Generic check;                  //object of Generic class created
    Savings save;                   //object of Savings class created
    CharRange input ('A', 'J'); 
//object of CharRange class created, will check for chars A-J
    char choice;
                save.status(true);

void withdraw(Savings &savings)
{   
    int balance;
    double dollars;
    savings.status(false);
    cin >> dollars;
    cin.ignore();
    if(!savings.withdraw(dollars))
        cout << "ERROR: Non-sufficient funds.\n\n";

}

in "Savings.cpp" I have:

#include <iostream>
#include "Savings.h"
using namespace std;

bool Savings::withdraw(double amount)
{ 
    if (balance < amount)
        return false;
    else if (balance < 25)
        return false;   //not enough in the account

    else 
    {
        balance -= amount;
        withdrawals++;
        transactions++;

        return true;
    }

}

void Savings::status(bool active)
 {  
     if (balance > 25)
     {
        bool active = true;
        cout << "Account is active\n";
        cout << "Enter the amount of the withdrawl: ";

     }

    else 
    {   
        bool active = false;
        cout << "Account is inactive\n";

    }     
 }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Is there more to main? Sounds like you want to put a while loop so you can cycle through it again once you are done with the transaction. Also, in your last post you asked about the boolean for active/inactive. I was saying you should put a member in your savings class (either public member or preferably private with getter/setter) and then toggle that when you run the status method (as you have it now, the active variable is local and will be destroyed when the method exits.

Edited 7 Years Ago by jonsca: n/a

Is there more to main? Sounds like you want to put a while loop so you can cycle through it again once you are done with the transaction. Also, in your last post you asked about the boolean for active/inactive. I was saying you should put a member in your savings class (either public member or preferably private with getter/setter) and then toggle that when you run the status method (as you have it now, the active variable is local and will be destroyed when the method exits.

#include <iostream>
#include <iomanip>		//needed for setprescision
#include <cctype>		//needed to use toupper, accepts a single char as its arguement and rtns the uppercase equivalent
#include "Generic.h"
#include "Savings.h"
#include "CharRange.h"	//CharRange class declaration

using namespace std;

//Function Prototypes
void displayMenu();
void deposit(Generic &);
void deposit(Savings &);
void withdraw(Generic &);
void withdraw(Savings &);

int main()
{
	Generic check;					//object of Generic class created
	Savings save;					//object of Savings class created
	CharRange input ('A', 'J');		
//object of CharRange class created, will check for chars A-G
	char choice;
	
	save.status(true);

	do
	{
		displayMenu();
		choice = input.getChar();		
//This returns only an uppercase A-G
		switch(choice)
		{
			case 'A': 
			case 'a': 
				cout << endl;
				cout << "The current balance is $";
				cout << save.getBalance() << endl;
				cout << endl;
				break;
			case 'B': 
			case 'b': 
				cout << endl;
				cout << "The current balance is $";
				cout << check.getBalance() << endl;
				cout << endl;
				break;
			case 'C':
			case 'c':
				
				cout << endl;
				//cout << "choose 'a' checkings transactions\n";
				//cout << "choose 'b' savings transactions\n";
				//cin >> transinput;
				//if (transinput = a)
					cout << "There have been ";
					cout << setw(1);
					cout << check.getTransactions();
					cout << " checkings transactions.\n";
				//if (transinput = b) 
					cout << "There have been ";
					cout << setw(1);
					cout << save.getTransactions();
					cout << " savings transactions.\n";
					cout << endl;
				break;
	
			case 'D':
					cout << endl;
					cout << "Interest earned for this period: $";
					cout << showpoint << setprecision(2);
					cout << save.getInterest() << endl;
					cout << endl;
					break;

			case 'E': 
					deposit(save);
					break;

			case 'F': 
				deposit(check);
				break;

			case 'G': 
				withdraw(save);
				break;

			case 'H': 
				withdraw(check);
				break;

			case 'I':	
				cout << endl;
				save.calcInt();
				cout << "Interest has been added to your savings account.\n";
				cout << "The amount of interest added to savings account: $";
				cout << setprecision(2);
				cout << save.getInterest() << endl;
				cout << endl;
				check.calcInt();
				cout << "Interest has been added to your checkings account.\n";
				cout << "The amount of interest added to checkings account: $";
				cout << check.getInterest() << endl;
					}
			}while(choice != 'J');
										
			return 0;				//return stmt causes fcn to end immediately
	
}

//************ displayMenu ***************//
//***displays user's menu on the screen***//

void displayMenu()
{
cout << "\n				Menu\n\n";
cout << "a) Savings Account balance		e) Savings Account deposit\n";
cout<<	"b) Checkings Account balance		f) Checkings Account deposit\n\n";
cout << "					g) Make a Savings Account withdrawal\n";
cout << "					h) Make a Checkings Account withdrawal\n";
cout << "c) Number of transactions\n";
cout << "d) Monthly interest earned\n";
cout << "i) Add interest for this month\n";
cout << "j) Exit\n";
cout << "\nEnter your choice: ";
}

Edited 7 Years Ago by Gem74: n/a

Can you trace where it is in the program when it's stopping? (i.e., where the menu is not displaying-- cause you do have a loop there). I see little things here and there but I think most of it looks ok.

Can you trace where it is in the program when it's stopping? (i.e., where the menu is not displaying-- cause you do have a loop there). I see little things here and there but I think most of it looks ok.

ok, so when i make a deposit, say $50, then i make a withdrawal, say $40, then the savings account balance is now $10 (which is considered inactive because it is less than $25). So then when i choose "g" to make a withdrawal from savings account, what is displayed is: "account is inactive" this is where it stops. The display menu does not redisplay and it should. The part that tells me "account is inactive" is perfect, but then I need it to tell me I cannot make a withdrawal because it is inactive and need the menu to redisplay, which it doesn't. greatly appreciate your help!

I believe the problem is in the member status fcn definition, in the else stmt.

void Savings::status(bool active)
 {	
	 if (balance > 25)
	 {
		bool active = true;
		cout << "Account is active\n";
		cout << "Enter the amount of the withdrawl: ";
		
	 }
	 
	else 
	{	
		bool active = false;
		cout << "Account is inactive\n";
		
	}
	  
 }

Ok, so what I was talking about before is that bool active should be in your class declaration:

class Savings
{
       private:
             bool active;              (along with other members)
       public:
             void SetStatus(bool act) {active = act;}
             bool GetStatus() const {return active;}
};
then scratch
void Savings::status()
 {	
	 
	 }
then have

void withdraw(Savings &savings)
{	
	int balance;
	double dollars;
	if(!savings.getStatus())
                return;
	cin >> dollars;
	cin.ignore();
	if(!savings.withdraw(dollars))
	{
             cout << "ERROR: Non-sufficient funds.\n\n";
	      savings.SetStatus(false);
         }		
}

(warning I didn't test it since I don't have your other code)
I'm not sure if that completely solves the problem but it helps you use your active status more effectively.

Edited 6 Years Ago by jonsca: n/a

It looks to me like the status function only writes the prompts that precede an input. The test for status in the code jonsca posted causes the input to be skipped so that might fix your problem.

(If not, try entering a value where the program appears to be paused -- I think its still asking for an amount to withdraw even though it shouldn't.)

Another question regarding implementation...is the $25 savings item really being handled like it is intended?

For example if my account starts with (or if it starts at $0 I deposit) $60. The balance is > $25 so it is active. Then I can withdraw $59, leaving $1 and making the account inactive. If the intent is to preserve a $25 minimum balance, shouldn't the withdraw prevent me from removing more than $35 to preserve the $25? (That's how my credit union works, the last $25 in my savings account cannot be removed at all unless I'm closing out the account.)

This article has been dead for over six months. Start a new discussion instead.