1

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

static int c;

class DAI   //DynamicArrayInput
{
public :
    void getCountry()
    {
    cout << "Country : ";
    cin >> Country;
    }

    void putCountry()
    {
    cout << Country << endl;
    }
    static int putCount()
    {
    return count;
    }
private :
    static int count;
    char Country[30];
};



int main()
{
vector< DAI > A;
DAI B[3];
//DAI * B;

cout << "Enter name of countries\n";
  for(int i=0; i < 3; i++)
  {
        //THIS DOESN'T WORK
    /*
    c++;   //c is a static variable declared at top
    B = new DAI[sizeof(DAI) * c/sizeof(DAI)];
    GIVES WRONG OUTPUT :
    Enter name of countries
    Country : a
    Country : b
    Country : c
    Printing name of countries


    c
    size of vector A is 3
    vector after initialisation:

    */  
    A.push_back(B[i]);
    B[i].getCountry(); 
  }
  cout << "Printing name of countries\n";
  for(int i=0; i < 3; i++)
  {
    B[i].putCountry();
  }

cout << "size of vector A is " << A.size() << "\
    \nvector after initialisation:" << endl;

return 0;
}


**/*
CORRECT OUTPUT WITH LIMITED ARRAY SIZE = 3:
*Enter name of countries
 Country : a
 Country : b
 Country : c
 Printing name of countries
 a
 b
 c
 size of vector A is 3
 vector after initialisation:*
*/**

I am just learning and this is not my homework, I dont want to be limited to any array size such as above but when i try to use the staements in the first for loop as mentioned it does not work i mean this does not works

c++;   //c is a static variable declared at top
B = new DAI[sizeof(DAI) * c/sizeof(DAI)];  //B is a pointer of type class DAI 

My aim is to use arrays of objects i like dynamically so that i can input any number of countries I don't want to use something like B[3] with something known array size but i am unable to do.

Please guide me, help me.

3
  • 2
    Use an std::vector or std::deque, which are auto-resizing containers. I see you are holding arrays in a vector - you could already have std::vector<std::vector<DAI> >. Commented Apr 27, 2011 at 15:27
  • Well I have used it in the program but Unable to proceed with it see first line of main Commented Apr 27, 2011 at 15:33
  • You don't initialize c anywhere. Commented Apr 27, 2011 at 15:37

2 Answers 2

2

c++;   //c is a static variable declared at top
B = new DAI[sizeof(DAI) * c/sizeof(DAI)];

Here you are confusing new with malloc. To allocate c elements of type DAI, just do

B = new DAI[c];

However, your best bet is to stay with std::vector, which handles size and capacity itself.

for (int i = 0; i != 10; ++i)
    A.push_back(DAI());

adds 10 DAIs to the vector A.

Edit
You can also create the vector with a number of DAIs from the start:

std::vector<DAI> A(10);

This also creates a vector with 10 elements, all at once. Unlike B[3], which always contains 3 elements, the vector can grow or shrink as needed without you having to handle the allocations.

Sign up to request clarification or add additional context in comments.

4 Comments

very nice! Thanks, Bo I will try B = new DAI[c] I dont get the second one DAI()
yes, your second idea works ...thanks a lot but whats DAI() is it a constructor call how does this works
Yes, it is a constructor creating new DAIs to be added to the vector. I also added a new example to my answer on how to create the vector with elements, without using a loop.
Thanks,I will accept your answer because I can accept only one at a time even though I haven't tried to implement the answer given below by Julio his answer also seems reasonable to me.
0

Your array is static DAI[3] v (v is in the process' stack). To be able to add an element dynamically, you need to change it with DAI[INITIAL_SZ] v = new DAI[INITIAL_SZ] (now v is in the heap) so that now you can realloc it when adding/deleting elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.