newbie stuck

so this is my first semester taking c++ and I am stuck with this assignment.
I am stuck in the deactivate account and setting account in the format
"xxxxx-xxxxx" (please see below)


Assingment:

Write a class definition for a BankAccount class that contains the following members:

• Member variables

o account ID (of the form xxxxx-xxxxx, where x is a digit)

o account holder name (e.g. John Joe)

o account balance

• Member methods

o Initialize(ID, name, balance): sets the account ID to a given ID, the
account holder name to the given string, and account balance to a
given number

o SetHolderName(name): sets the account holder name to the given name

o IncreaseBalance(amount): increases balance by the given amount

o DecreaseBalance(amount): if the given amount is no more than the
balance, remove it from the balance and return true; otherwise,
return false

o Deactivate: deactivates the account by setting the account ID to
00000-00000, holder name to the empty string, and balance to 0

o IsActive: returns true if the account is active and false otherwise
(an account is inactive if the account ID is 00000-00000 and the
holder name is the empty string

o Print: displays the state of the account (values of the member
variables in an organized format)

o GetID: gets the account ID

o GetHolderName: gets the account holder name

o GetBalance: gets the account balance


This is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
 
using namespace std;


class BankAccount {
public:
int accId;
string HolderName;
float Balance; 

    void setHolderName (string n) {
       HolderName=n;
    }
    void setaccId ( int x ) {
        accId=x;
    }
    void setBalance ( float b) {
        Balance = b;
    }
    
    string getHolderName () {
        return HolderName;
    }
    float getBalance () {
        return Balance;
    }
    int getaccId () {
        return accId;
    }
    void IncreaseBalance ( float amount) { 
        Balance += amount;
        }
    void DecreaseBalance ( float amount)
    {
        char pause;
        if (Balance<= amount)
        {
            cout<< '\n' << "balance cannot be decreased by this amount because" << accId << "does not have enough funds\n";
            cout<< '\n' << "Press enter to continue" << endl; 
            cin.get(pause);
            return;
    }
        Balance -= amount;
        return ;
        }
    
        
   
};
int main ()
{}


Any help would be greatly appreciated
Last edited on
Hello myruuh,

As a start:


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Inside "main" I would start with calling a function to display a menu, get the user's choice and verify that it is a valid entry before returning the result back to main. I would likely follow that with a switch to direct the program flow.

Something to work on while I take a better look at what you need and what you have done.

Hope that helps,

Andy
will do thank you!
Hello myruuh,

As I look over what you have you are missing the header file "string".

//using namespace std; // <--- Best not to use.

By putting your variables in the "public:" section of the class you are defeating the purpose of the class and your member functions become mostly useless because any part of the program can access these variables.

To start with the class would work better as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class BankAccount
{
	public:

		void setHolderName(std::string n);
		void setaccId(int x);
		void setBalance(double b);
		std::string getHolderName();
		double getBalance();
		int getaccId();
		void IncreaseBalance(double amount);
		void DecreaseBalance(double amount);

	private:
		std::string accId;  // <--- Changed.
		std::string HolderName;
		double Balance;
};

BankAccount::BankAccount()  // <--- Default ctor.
{
	Balance = 0.0;
}

 // <--- Here you may need an overloaded ctor.

void BankAccount::setHolderName(std::string n)
{
	HolderName = n;
}

void BankAccount::DecreaseBalance(double amount)
{
	char pause;

	if (Balance <= amount)
	{
		std::cout << '\n' << "balance cannot be decreased by this amount because" << accId << "does not have enough funds\n";
		std::cout << '\n' << "Press enter to continue" << std::endl;
		std::cin.get(pause);
		return;
	}
	Balance -= amount;
	//return; // <--- "return" statement nor needed.
}

I did not include all the functions, but enough to give you an idea.

The other method that is more often used is to put the class in a header file and the member functions in a ".cpp" file, but you may not be there yet.

The directions say:
account ID (of the form xxxxx-xxxxx, where x is a digit)
. Given the example the variable would work better as a "std::string". As an int it will subtract the rhs from the lhs before it stores the result.

To store as an "int" it would have to be in two different variables in order to put the "-" back in later.

Something to think about is what to do with the information in the class once you have set the values of the variables. The best choice would be a vector of type "BankAccount", but an array would also work.

When you have something post the new code and we will see what happens.

Hope that helps,

Andy
Topic archived. No new replies allowed.