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.

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. iostream was included. However, I don't know how I can add a member to Account dynamically. If the user says Nmbr = 222 then it will just add the member Nmbr rather then 222 to Account...

yes, I have added the folloing lines on top:

#include <iostream>
using namespace std;
using std::cin;
using std::cout;

However, it still won't link the cin input to the Account member name. Do I need to make any adjustments to my code?

Edited 3 Years Ago by Dani: Formatting fixed

        int Nmbr;    
        int Bal; 
        char Stat;
        cin >> Nmbr;    
        cin >> Bal;   
        cin >> Stat;
        Account Nmbr; 

This code seems to attempt to create an int named Nmbr and also an Account named Nmbr. This makes no sense. You cannot give two different objects the exact same name.

Are you trying to create, at runtime, an Account object with a name that the user chose an runtime? If so, it's impossible; the name of the object does not exist at runtime. It's used at compile time so the compiler knows which object you want to do something with. After that, it ceases to exist.

However, I don't know how I can add a member to Account dynamically

Or are you trying to change the class Account at runtime? Such that before it looked like this:

class Account {   
public:   
            char Status;   
            int Balance;   
};

and now it looks like this?

class Account {   
public:   
            int Nmbr;
            char Status;   
            int Balance;   
};

That's also impossible.

Edited 3 Years Ago by Moschops

Well, my plan was that the user can create several Account members and also name them. Also, a variable of Account is called Number and has the same name as the Account member. This would make it easy for me to identify the Account members. So for example makes the following input for cin to Nmbr: 222. 222 will then be the name for the Account member and value for Account variable Number. But I guess this won't be possible then as you explained.

You are kind of doing it wrong.. What you want to do, is create an Array of objects, the size of this array can then be determined at run-time.. Something like the following:

#include <iostream>
using namespace std;

class Foo {

    public:
        static int count; 
        Foo() {

        }

        Foo(int i) {
            std::cout << "I am object no: " << count << std::endl;
            count++;    
        }


};  

int Foo::count = 0; 

int main() {
    // your code goes here
    std::size_t number; 
    std::cout << "Enter the number of accounts you want to create: ";
    std::cin >> number; 

    Foo* f[number];

    for(unsigned i=0; (i < 10); i++)
    {
        f[i] = new Foo(i);
    }
    return 0;
}

I am object no: 0
I am object no: 1
I am object no: 2
I am object no: 3
I am object no: 4
I am object no: 5
I am object no: 6
I am object no: 7
I am object no: 8

http://ideone.com/svNp9S
I am object no: 9

222 will then be the name for the Account member and value for Account variable Number

If you had a variable named "someName", like this:

Account someName;

how were you going to retrieve it when the user said he wanted the variable named someName?

As Phorce stated, you've simply misunderstood what variables names are for. If you want to account to have a number, give it a number:

class Account {   
public:   
            int Nmbr;
            char Status;   
            int Balance;   
};

If you want to user to set that Nmbr, get the value from the user and set it.

cin >> userInput;
someAccount.Nmbr = userInput;

Edited 3 Years Ago by Moschops

One way to accomplish what you want might be a little complicated. Make an array of Account objects and add a member to Account class that is a string in which you can store the instrance's name. Then if you say you want to work with account 222 you would search the array for the name that is "222".

But as others have said once you compile the program the name of the object is written in stone and cannot be changed.

Probably was searching the web and found this page ?

So @nonc...

Welcome to Dani's place and her C++ help forum.

You may not know how to start a new thread?

Click on the BOX at the top, or bottom, that says:

Start New Discussion

And then enter a short title that is consistent with your C++ programming question ...

Then enter your question and the example of your code where you think you are 'stuck'.

Make sure you select all the code block ...

and then ...
tab/indent it all ...

to preserve the proper indentation in your example code.

And don't forget to fix up the spelling and grammar before submitting your 'page'.

This article has been dead for over six months. Start a new discussion instead.