Create Class Object dynamically through cin

Nov 17, 2013 at 3:34pm
Hi guys,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
}
Nov 17, 2013 at 3:56pm
R9tommy wrote:
How can I diynamically create an object through cin and give their variables a value through cin?

The best method would probably be to use pointers to create a dynamic object and then accept the values of member functions thorough cin.
Last edited on Nov 17, 2013 at 3:56pm
Nov 17, 2013 at 3:59pm
First off, that won't compile.
You have Nmbr defined as both an int (line 8) and an instance of Account (line 15).

// creates the object based on user input for 'Nmbr'

No, it doesn't work that way.

What you probably want is an explicit constructor such as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  class Account 
{   
public:   
    Account (int num,int bal,char stat);
    int nmbr;
    char Status;   
    int Balance;   
};

Account (int num,int bal,char stat)
{  nmbr = num; 
    Balance = bal; 
    Status = stat; 
}


Then after you've collected Nmbr, Bal and Stat, you can do:
1
2
 
  Account acct (Nmbr, Bal, Stat);


Nov 17, 2013 at 4:01pm
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.
Nov 17, 2013 at 4:10pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Account 
{   
public:   
    int nmbr;      // nmbr is an attribute of the account
    char Status;   
    int Balance;   
    Account (int num,int bal,char stat);  //  Explicit constructor
};

int main() 
{   int Nmbr;    
     int Bal; 
     char Stat;
     
    // Collect the input
    cin >> Nmbr;    
    cin >> Bal;   
    cin >> Stat;
       
    Account acct (Nmbr, Bal, Stat);  // Create and initialize the object 
    cout << acct.nmbr << endl;      // display Account Status
    cout << acct.Balance << endl;  //display Account Balance
    return 0;
}
Nov 17, 2013 at 4:17pm
I forgot that dynamic values are possible even without pointers! LOL
Nov 17, 2013 at 4:17pm
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?
Nov 18, 2013 at 12:35am
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.

Here's a simple example:
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
  int num_accts = 0;
  const int max_accts = 10;
  Accounts accts[max_accts];
...
void AddAccount ()
{  Account * acct;
    if (num-accts == max_accts)
    {  cout << "Too many accounts" << endl;
        return;
    }
    acct = accts[num_accts];  // create a pointer to the account we want to initialize
    num_accts++;
    cin >> acct->nmbr;    
    cin >> acct->Balance;   
    cin >> acct->Status;
}

// Seach for a specific account
Account * FindAccount (int num)
{  for (int i=0; i<num_accts; i++)
      if (accts[i].nmbr == num)
        return &accts[i];  // Found match, return pointer to account entry
    return NULL;  // not found
}


Topic archived. No new replies allowed.