#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.
std::vectororstd::deque, which are auto-resizing containers. I see you are holding arrays in a vector - you could already havestd::vector<std::vector<DAI> >.