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

using namespace std;



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;           //HOW IS THE POINTER B POINTING TO VECTOR A EVEN THOUGH I                  //HAVE NOT INITIALIZED IT LIKE B = A
                   //OR DAI * B;
int answer;

cout << "How many countries do you want to enter : ";
cin >> answer;
  for(int i=0; i < answer; i++)
  {
	A.push_back(DAI());
	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();

return 0;
}

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.

It worked on g++ compiler but it did not work on devC++ as the pointer
DAI * B;
is not initialised then how do I try to build my program I can't do
DAI * B = A;
also because DAI is a class type and A is a vector( vector<DAI> A)
please help??

Arrays and vectors are not the same and are not interchangable. You can not make (in your case) a DAI* point to a vector<DAI>. They are incompatible types.

You will need to do this as either strictly vector or strictly array. If you choose array, I suggest that you read up on the new[] and delete[] operators.

class Sample {
  //class members
};

int main() {
  Sample *sampleArray = NULL;

  //prompt user for array size

  sampleArray = new Sample[inputSize];

  //...

  delete[] sampleArray;

  return 0;
}

Why are you even declaring aonther DAI? If you want to youse the getCountry function all you have to do is

for(int i=0; i < answer; i++)
{
    A.push_back(DAI());
    A[i].getCountry();
}

Thanks a lot,

I changed two lines as you said
A.getCountry();
A.putCountry();

then I had to add another two lines

before return 0; as below

fflush(stdin);
getchar();

It worked
but is fflush(stdin); really required , is it good to do it this way to flush the newline in the buffer
OR is there any other way around


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

using namespace std;



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;           //HOW IS THE POINTER B POINTING TO VECTOR A EVEN THOUGH I                  //HAVE NOT INITIALIZED IT LIKE B = A
                   //OR DAI * B;
int answer;

cout << "How many countries do you want to enter : ";
cin >> answer;
  for(int i=0; i < answer; i++)
  {
	A.push_back(DAI());
	A[i].getCountry(); 
  }
  cout << "Printing name of countries\n";
  for(int i=0; i < 3; i++)
  {
	A[i].putCountry();
  }

cout << "size of vector A is " << A.size();
fflush(stdin);
getchar();
return 0;
}

No. fflush() is for output streams. It will work for input streams on some implementations/compilers, but it's intended for output streams.

Use std::istream::ignore() instead...

No. fflush() is for output streams. It will work for input streams on some implementations/compilers, but it's intended for output streams.

Use std::istream::ignore() instead...

Thanks,

I used

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Its works pretty well thanks again.I hope its OK to use it.

Looks like you did your homework. That's the correct way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.