#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;
}

Edited 5 Years Ago by Fbody: n/a

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();
}

Edited 5 Years Ago by NathanOliver: n/a

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;
}

Edited 5 Years Ago by munitjsr2: n/a

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...

Edited 5 Years Ago by Fbody: n/a

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.

Edited 5 Years Ago by munitjsr2: n/a

This question has already been answered. Start a new discussion instead.