Read text files.

Hello, I am trying to copy names in to a dynamic array. The first integer shows how many names there are. I am getting this error "'std::basic_istream<char>::int_type {aka int}' to 'char*' [-fpermissive]".

Please help thank u.

8
Low,Steve
Mcknight,Laura
Babko,Egor
Chau,Phillip
Chin,Dugan
Colmanshepherd,Craiges
Ding,Fengkun
Dollynchuk,Nicholis


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <iostream>
#include <fstream>
#include <string.h>

//swap(names[i], names[j]);


using namespace std;

int main()
{
	ifstream names;
	names.open("names.txt");
	
	int total  = 0;
	
	char** arrayNames = new char*[total];
	
	while (!names.eof())
	{
		names >> total;
		cout << total << endl;
		
		for ( int k = 0; k < total; k++)
		{
			arrayNames[k] = names.get();
		}
		
		for (int j = 0; j < total; j++)
			   delete[] arrayNames[j];	
			   
	}
	
	names.close();	
	
	return 0;
}

You omitted the important part of the message
26:30: error: invalid conversion from ‘std::basic_istream<char>::int_type {aka int}’ to ‘char*’ [-fpermissive]
That refers to arrayNames[k] = names.get();
http://www.cplusplus.com/reference/istream/istream/get/ as you can see you used the version that returns just one character

To read a line use `getline()' http://www.cplusplus.com/reference/string/string/getline/
1
2
3
4
5
int total;
names>>total;
std::vector< std::string > arrayNames(total);
for(int K=0; K<total; ++K)
   std::getline( std::cin, arrayNames[K] );
(the new and delete are encapsulated in vector)
Last edited on
Topic archived. No new replies allowed.