Need help with class problem <3

Hello everyone,

Me and my girlfriends have been having a really hard time with this C++ problem (we would love it if you could help us!). Thanks in advance ;)



[b]
Overview


For this assignment, implement and use the methods for a class called Account.
The Account class

Use the following class definition:

class Account
{
public:
Account( char [], char, double); //constructor

void printAccount();
void changeBalance( double );

void setAcctType( char );

char getAcctType();
double getBalance();

private:
char name[50];
char acctType;
double balance;
};

[b]Data Members


The data members for the class are:

name this holds the name of the account holder

acctType this holds 'S' for a savings account or 'C' for a checking account

balance this holds the account balance






Constructor

The constructor for the class should initialize the data members using the passed in arguments. It takes 3 arguments: a character array with the name of the account holder, a character that represents the type of account, and a double that holds the account balance. Use the setAcctType method to initialize the account type data member (this will help to guard against invalid initial values). If the changeBalance method is used to initialize the balance data member, don't forget to initialize the data member to 0 before calling the method.
Methods
void printAccount()

This method displays the account information. It takes no arguments and returns nothing. It should display the information as follows:

Name: Barney Rubble Checking Account Balance: $ 1234.56

or

Name: Fred Flintstone Savings Account Balance: $ 1234.56






void changeBalance( double amountToChange )

This method updates the balance of an account. It takes one argument: a double that represents the amount to change the account's balance. It returns nothing.

The argument will have a negative value if a withdrawl was made on the account or a positive value if a deposit was made to the account. In either case, simply add the passed in value to the current balance. After applying the update, if the resulting balance is less than 0, display a message that the account is overdrawn and that a fee of $10 will be added to the balance and then apply the fee to the balance.



void setAcctType( char newType )

This method changes an account type. It takes one argument: a character that represents the new account type. It returns nothing.

If the passed in account type is not 'S', 's', 'C', or 'c', display an error message and set the account type to 'C'. If the passed in account type is 'S', 's', 'C', or 'c', update the account type but *ONLY* use uppercase characters.





char getAcctType()

This method returns an account type. It takes no arguments.





double getBalance()

This method returns an account balance. It takes no arguments.
main()





In main(), create 5 Account objects. They should contain the values:

The first account should have your name, an account type of 'C', and a balance of 113019.77

The second account should have the name of your recitation TA, an account type of 'S', and a balance of 1234.50

The third account should have the name "Temperance Brennan", an account type of 's', and balance of 82.12

The fourth account should have the name "Seeley Booth", an account type of 'c', and a balance of 3869.00

The fifth employee should have the name "Zack Addy", an account type of 'C', and a balance of 71940.76

The rest of main() will include using the various methods on each of the 5 Account objects. Display a label similar to "The first Account object" before anything is outputted for each of the objects.

For the first account, display the account information, increase the account balance by 939.39, and display the account information once again.

For the second account, display the account information, decrease the account balance by 240.00, and display the account information once again.

For the third account, display the account information, change the account type to 'c', increment the account balance by 82000.12, and display the account information once again.

For the fourth account, display the account information, try to change the account type to an invalid value (the choice is up to you), decrease the account balance by 43321.98, display only the account balance, and display the account information once again.

For the fifth employee, display the account information, change the account balance by applying a withdrawl (the actual amount is up to you), display the account information, change the account balance by applying a deposit (the actual amount is up to you), and then display the account information once again.
Programming Note

Each method must have a documentation box like a function.


Last edited on
Um... That's a lot to read over.

1. Format your post, so that the code is easier to read. (Look to the right of the edit box when posting for formatting options.)
2. Please explain a smaller problem your having; shorten your post some. Do you know how to start your program? If not, start there. If you do, then let us know where your having the problem.

I can't speak for everyone, but I don't want to do your homework, I want to help you learn how to program so you can do your own homework. (You know, give a man a fish...)
Last edited on
I understand this is what i have so far....
1
2
3
#include <iostream>
#include <iomanip>
#include <fstream> 


using namespace std;

class Acct
{
public:
Account( char [], char, double); //constructor

void printAccount();
1
2
3
4
5
6
7
8
9
10
11
void changeBalance( double );

  void setAcctType( char );

  char getAcctType();
  double getBalance();

private:
  char name[50];
  char acctType;
  double balance;

};

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
void printAccount()
void changeBalance(double amountToChange)
void setAcctType(char newType)
double getBalance()
char getAcctType()


int main()
{
Acct account1 = Acct( "Dylan, Sylvia", 'C', 113019.77 );
Acct account2 = Acct( "Taylor, Jon", 'C', 1234.50 );
Acct account3 = Acct( "Temperance, Brennan", 'C', 82.12 );
Acct account4 = Acct( "Seeley, Booth", 'C', 3869.00 );
Acct account5 = Acct( "Zach, Addy", 'C', 71940.76 );

account1.printAcct();
account2.printAcct();
account3.printAcct();
account4.printAcct();
account5.printAcct();
}

i just don't understand how to do the.....

void printAccount()
void changeBalance()
void setAcctType()
char getAcctType()
double getBalance()

I started the void printAccount(), and i got this

void Acct::printAccount()
{
if (acctType == 'C')
cout << name << "/t"<< "Checking Account Balance:  $"<< balance; 
else
cout << name << "/t"<< "Savings Account Balance:  $"<< balance; 
} 
Last edited on
Mathhead200 wrote:
Format your post, so that the code is easier to read.
1
2
3
4
5
6
7
//Like this, between [code] tags
#include <iostream>

int main() {
    std::cout << "Hello, world";
    return 0;
}


printAccount() looks right. Next would be your void Acct::changeBalance(double amount) method. The description you posted looked pretty self-explanatory.
agnk33 wrote:
This method updates the balance of an account. It takes one argument: a double that represents the amount to change the account's balance. It returns nothing.

The argument will have a negative value if a withdrawl was made on the account or a positive value if a deposit was made to the account. In either case, simply add the passed in value to the current balance. After applying the update, if the resulting balance is less than 0, display a message that the account is overdrawn and that a fee of $10 will be added to the balance and then apply the fee to the balance.
Have you attempted this one yet?


Just as a note, you could make your print method a bit more compact by noticing that most of the text is the same in either case:
1
2
3
void Acct::printAccount() {
    cout << name << '\t' << (acctType == 'C' ? "Checking" : "Savings") << " Account Balance: $" << balance;
}
If this looks more complicated don't worry about it. Your method works just as well. Programmers just tend to try and not duplicate code whenever possible.
Last edited on
I hope the program is easier to read. It is my first time using this site. I tried to attempt these other four constructors but nothing is helping me. My teacher never taught us this completely.

I really am just completely lost. I have been trying to attempt this program since 6 o'clock and I feel like I haven't accomplished anything yet.

I would greatly appreciate it if you can help me. I have never been so lost in c++.
Last edited on
So you have something like this (at least) right? If not, go ahead and copy what you need. Then we can start to fill in the methods one by one. Then compile and run after each method to check if it's working.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#include <iostream>
#include <cstdlib>

using namespace std;


class Acct {
 private:
	char name[50];
	char acctType;
	double balance;

 public:
	Acct(char[], char, double);
	
	void printAccount() const; //print the account information to std::cout
	char getAcctType() const; //return the account type
	double getBalance() const; //return the account balance
	void setAcctType(char); //change the account type to a given type
	void changeBalance(double); //change the account balance by a given amount
};


//Acct constructor definitions
/**
 * n - The account holders name. Must be 49 characters or less!
 * at - The account type. Must be either 'c' for checking, or 's' for savings!
 * b - The account's initial balance.
 */
Acct::Acct(char n[], char at, double b) : acctType(at), balance(b) {
	//copy as much of n into name as possible
	int LEN = strlen(n);
	if( LEN >= 50 )
		LEN = 50 - 1;
	int i = 0;
	while( i < LEN ) {
		name[i] = n[i];
		i++;
	}
	name[i + 1] = '\0';
}


//Acct method definitions
void Acct::printAccount() const {
	//TODO, method stub
}

char Acct::getAcctType() const {
	//TODO, method stub
	return '\0';
}

double Acct::getBalance() const {
	//TODO, method stub
	return 0.;
}

void Acct::setAcctType(char at) {
	//TODO, method stub
}

void Acct::changeBalance(double amount) {
	//TODO, method stub
}

//-----------------------------------------------------------------------------

int main() {
	//create a checking account "acct" under the name agnk33 with an initial balance of $0.00
	Acct acct("agnk33", 'c', 0.00);
	
	//print acct's info to cout
	acct.printAccount();
	
	//print scct's type (as a char) and scct's balance (as a double)
	cout << acct.getAcctType() << ' '
	     << acct.getBalance() << endl;
		 
	//set acct's account type to savings
	acct.setAcctType('s');
	
	//add $100.00 to acct's balance
	acct.changeBalance(100.00);
	
	//print acct's info to cout (again)
	acct.printAccount();
	
	//end the program
	return 0;
}
Last edited on
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
[output]#include <iostream>
#include <cstdlib>

using namespace std;


class Acct {
 private:
	char name[50];
	char acctType;
	double balance;

 public:
	Acct(char[], char, double);
	
	void printAccount() const; //print the account information to std::cout
	char getAcctType() const; //return the account type
	double getBalance() const; //return the account balance
	void setAcctType(char); //change the account type to a given type
	void changeBalance(double); //change the account balance by a given amount
};


//Acct constructor definitions
/**
 * n - The account holders name. Must be 49 characters or less!
 * at - The account type. Must be either 'c' for checking, or 's' for savings!
 * b - The account's initial balance.
 */
Acct::Acct(char n[], char at, double b) : acctType(at), balance(b) {
	//copy as much of n into name as possible
	int LEN = strlen(n);
	if( LEN >= 50 )
		LEN = 50 - 1;
	int i = 0;
	while( i < LEN ) {
		name[i] = n[i];
		i++;
	}
	name[i + 1] = '\0';
}


//Acct method definitions
void Acct::printAccount() const {

if (acctType == 'C')
cout << "Name: " << name << "/t"<< "Checking Account Balance:  $"<< balance; 
else
cout << "Name: " << name << "/t"<< "Savings Account Balance:  $"<< balance; 


}

char Acct::getAcctType() const {
	//TODO, method stub
	return '\0';
}

double Acct::getBalance() const {
	return balance;
	return 0.;
}

void Acct::setAcctType(char at) {
	//TODO, method stub
}

void Acct::changeBalance(double amount) {
	//TODO, method stub
}

//-----------------------------------------------------------------------------

int main() {
// this has to stay in the int main too, right?

Acct account1 = Acct( "agnk33,agnk33", 'C', 113019.77 );
Acct account2 = Acct( "Naga, lilu", 'C', 1234.50 );
Acct account3 = Acct( "Temperance, Brennan", 'C', 82.12 );
Acct account4 = Acct( "Seeley, Booth", 'C', 3869.00 );
Acct account5 = Acct( "Zach, Addy", 'C', 71940.76 );

account1.printAccount();

account2.printAccount();
account3.printAccount();
account4.printAccount();
account5.printAccount();



	//create a checking account "acct" under the name agnk33 with an initial balance of $0.00
	Acct acct("agnk33", 'c', 0.00);
	
	//print acct's info to cout
	acct.printAccount();
	
	//print scct's type (as a char) and scct's balance (as a double)
	cout << acct.getAcctType() << ' '
	     << acct.getBalance() << endl;
		 
	//set acct's account type to savings
	acct.setAcctType('s');
	
	//add $100.00 to acct's balance
	acct.changeBalance(100.00);
	
	//print acct's info to cout (again)
	acct.printAccount();
	
	//end the program
	return 0;
[/output]
Last edited on
I just wrote my own test (driver) program. You can add whatever you need to main and delete my stuff from there once you know your class works properly. That other stuff makes a nice test too though, so it should be fine if you left it in there during the testing phase.

Also, notice how I tabbed in (added a left margin of whitespace) when inside of the functions/methods. You should make a habit of this, as it makes your program MUCH easier to read and hence debug and update. Another tab is often used when inside of if-statemnets and loops as well. In fact pretty much any time you see a pair of {} you should tab the inside code in once (by one extra tab.)
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
//Some example code to show what I mean above:

//Here we are not in a function, we don't need to tab in.
#include <iostream>

using namespace std;

int main() {
	//We are in a function, let's tab in one time for each of these lines.
	int x, y;
	cin >> x >> y;
	if( x > y ) {
		//We are now in an if-statement, let's tab in one more time (that's twice) for each of these lines.
		//Note that these {} (braces) are optional, because we have only one statement (line of code ending in a ; (semi-colon) within our if-statement.
		//This applies to "else" and loops as well, but not functions or classes!
		//However, even if you don't use the {} you should still tab in an extra time for the following line.
		cout << x << " is greater then " << y << '.';
	} else if( y > x ) { //You could put this else on the next line (this is just my style), but you should tab it in once because your still in a function. The else's should line up with the if's.
		//Notice we tab in twice again here, we're in another block of code (a block of code is code within {}.)
		cout << y << " is greater then " << x << '.';
	} else {
		cout << x << " and " << y << " are equal.";
	}
	//We are no longer in an inner block, we are back, just inside the function.
	//Now we only tab in once again.
	cout << endl;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.