I am trying to read a sentence from a text file and store it in a 2D array. I am using the function cin.getline. I am trying to store each sentence of the text file to a row of the 2D array.read is my ifstream object. Below is a sample of my code
for (int i = 0; i < 7; i++)
{
int k=0;
read.getline(people.wishlist[i][k], MAX); // store in row 0 when i is 0
}
// what my text file looks like:
// Hey how is your day
// whats up
The error message I am getting:
error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)':
cannot convert parameter 1 from 'char' to 'char *'
Can someone suggest a good method to solve this problem for me? Thanks
The problem is that on a 2D char array
people.wishlist[i][k]stands for a singlechar(ith row andkth column), butgetlineexpects a string of themchar*.You need a pointer to a 1D char array, which you can get indexing just one other dimension. (with
i) You could try it this way: