Class Structures

Hello, I am new to this site but heard there are some people on here who can be of some great help, so I though I might throw a question out there. I am new to computer language and am taking a c++ class as my first taste of it so bear with me please. I was given a program and was asked to make a header file for it using a class structure. I put the code together but it is not working for me. The notes to the sides of all my code were the directions I received. Can anyone PLEASE tell me where I went wrong?
#include<iostream>

using namespace std;

class BankAccount
{

private:
int acctNumber;
string name;
double balance;


public:

BankAccount(); //constructor . Initialize acctNumberto 0, name to “Unknown”, balance to 0.0.
BankAccount(int aNum, string aName, double amount); // overloaded constructor . Initialize acctNumber to a Num name to aName and balance to amount.
void setAcctNumber(int aNum); //sets instance variable acctNumber
void setName(string aName); //sets instance variable name.
void setBalance(double amount); //sets instance variable balance.
int getAcctNumber(); // returns the value of instance variable acctNumber.
string getName(); //returns the value of instance variable name
double getBalance(); //returns the value of instance variable balance.
void deposit(double amount); //adds amount to the balance.
void withdraw(double amount); //subtracts amountfrom the balance.
void displayData(); //displays the current values of all instance variables.


};
BankAccount::BankAccount()
{
acctNumber = 0 ;
name = " unknown " ;
}
BankAccount::BankAccount(int aNum, string aName, double amount) // overloaded constructor . Initialize acctNumber to a Num name to aName and balance to amount.
{
acctNumber = aNum ;
name = aName;
balance = amount ;
}
void BankAccount::setAcctNumber(int aNum) //sets instance variable acctNumber
{
aNum = 0 ;
}
void BankAccount::setName(string aName) //sets instance variable name.
{
aName ;
}
void BankAccount::setBalance(double amount) //sets instance variable balance.
{
amount = 0.0 ;
}
int BankAccount::getAcctNumber() // returns the value of instance variable acctNumber.
{
return acctNumber;
}
string BankAccount::getName() //returns the value of instance variable name
{
return name ;
}
double BankAccount::getBalance() //returnsthe value of instance variable balance.
{
return balance ;
}
void BankAccount::deposit(double amount) //adds amount to the balance.
{
balance += amount ;
}
void BankAccount::withdraw(double amount) //subtracts amountfrom the balance.
{
balance -= amount ;
};
void BankAccount::displayData() //displaysthe current values of all instance variables.
{
cout << " Account number : " << acctNumber << endl;
cout << " Name : " << name << endl;
cout << " Balance : " << balance << endl;
}
1
2
3
4
 void BankAccount::setName(string aName) //sets instance variable name.
 {
 aName ;    //What is this supposed to do?
 }


If your code snippet is from a header, I suggest removing "using namespace std;" and using the "std::" scope, e.g. "std::cout" and "std::cin," or the "using std::" line, e.g. "using std::cout;" and "using std::cin;".
using namespace std; is all we have used this far in the CSC100 class I am taking. I appreciate the knowlege very much, but I have to stick to my class standards.

void BankAccount::setName(string aName) //sets instance variable name.
{
aName ; //What is this supposed to do?
}

I was not sure what to do with that since the name is something that will be entered by a user. This header goes along with a pre-written program that we must make the header work for. This is the output I get right now.


Welcome to your bank

1. Make a deposit
2. Make a withdrawal
3. Set the account name
4. Set the account number
5. Display account information
6. Exit

Please enter your choice: 5

Account number : 0
Name : unknown
Balance : -9.25596e+061

Welcome to your bank

1. Make a deposit
2. Make a withdrawal
3. Set the account name
4. Set the account number
5. Display account information
6. Exit

Please enter your choice:
You might have a case of junk assignment. In your constructor, include "balance = 0.0;" to initialize the variable and see what happens.
That totaly worked ! Thankyou ! I am still having a problem of the string not being set when entered by the user. I think I gotta figure out to do with that aName

/* OUTPUT


Welcome to your bank

1. Make a deposit
2. Make a withdrawal
3. Set the account name
4. Set the account number
5. Display account information
6. Exit

Please enter your choice: 3

Enter the name: B Simpson

Welcome to your bank

1. Make a deposit
2. Make a withdrawal
3. Set the account name
4. Set the account number
5. Display account information
6. Exit

Please enter your choice: 5

Account number : 0
Name : unknown
Balance : 0

Welcome to your bank

1. Make a deposit
2. Make a withdrawal
3. Set the account name
4. Set the account number
5. Display account information
6. Exit

Please enter your choice:
Have you learned how to use the "cin" object?
1
2
3
4
 void BankAccount::setName(string aName) //sets instance variable name.
 {
      getline(cin, name); //Take user input and assign string to name member
 }

That did not work. The spot for string is now just empty instead of saying " unknown "
Welp, if I assume that you are using "cin >>" when the user makes a choice, then I suggest one of the two:

1. Include "cin.ignore()" after the choice making instruction to grab that hanging newline, so the getline function does not catch it.

2. Replace getline with one or two "cin >>" since cin stops at the first space.
A function named setName that takes a string as an argument is clearly not going to be asking for user input. What it is going to be doing is assigning the value represented by the argument to the object representing the name in the class.

1
2
3
4
void BankAccount::setName(string aName)
{
    name = aName ;
}
closed account (D80DSL3A)
Try
1
2
3
4
void BankAccount::setName(string aName) //sets instance variable name.
 {
      name = aName;// this actually sets the instance name
 }
You rock ! That was it . Thank a million !
Topic archived. No new replies allowed.