I have a class called Account with variables called Balance and Status. The Member Name is given by the User through cin and represents an the Account Number. How can I diynamically create an object through cin and give their variables a value through cin?
I have been looking for hours and just cannot figure it out. Any help is appreciated. Thanks guys.
Here's my code. It won't compile as I'm just trying to show what I want to do.
class Account {
public:
char Status;
int Balance;
};
int main() {
int Nmbr;
int Bal;
char Stat;
cin >> Nmbr;
cin >> Bal;
cin >> Stat;
Account Nmbr; // creates the object based on user input for 'Nmbr'
Nmbr.Status = Stat; // add the cin input 'Stat' to the variable Status to the newly created Account member
Nmbr.Balance = Bal; // add the cin input 'Bal' to the variable Balance to the newly creted Account member
cout << Nmbr.Number << endl; // display Account Status
cout << Nmbr.Balance << endl; //display Account Balance
}
Thanks for your answer. I'm relatively new to C++. Would you mind showing me a simple code including pointers as described in your response? That would help me a lot in understanding. I appreciate it.
Thanks AbstractionAnon for making an easy understandable code. However, let's say the user created 5 different Account objects called Acct1, Acct2, Acct3, Acct4, Acct5, and stores these values in nmbr: 111, 222, 333, 444, 555. How will I later know which nmbr is stored in which Account member since I cannot assign the nmbr to the Account member name?
It's best to create an array or vector of accounts rather than creating different named instances. Then it becomes a simple matter to serach the array or vector.