Hello

I am making a function that reads a text file stores each line into a string array. My problem is I am getting errors with the function but I do not understand what I am doing wrong or how to fix it?

Can you take a look & see what I need to change? :)

the error is

incompatible types in assignment of `std::string' to `std::string[((unsigned int)((int)pic_count))]'

my program:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace std;

string read(string file, int& counter);

int main() {

	int pic_count = 0;
	int usedPic_count = 0;
	string pic_array[pic_count];
	string used_pics[usedPic_count];


	pic_array = read("data.txt",pic_count);   // error 
	used_pics = read("usedPictures.txt",usedPic_count); // error

	return 0;
}

string read(string file, int& counter) {

	string array[999];
	ifstream infile;

	infile.open(file); // error

	if (!infile) {
		cout << "Failed to open file";
		return 0;
	}

	while (infile) {
		getline(infile,array[counter],'\n');
		counter++;
	}

	return (array); // error
}

Recommended Answers

All 7 Replies

You do know that your array is of size 0 in this code :

string pic_array[pic_count];
	string used_pics[usedPic_count];

for your read function, use char *filename

Thanks

I changed the size of the string arrays to 299 & added the char* filename :)

Do you know why I am getting errors for my read function? :)

thanks

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace std;

string read(char* file, int& counter);

int main() {

	int pic_count = 0;
	int usedPic_count = 0;
	string pic_array[299];
	string used_pics[299]


	pic_array = read("data.txt",pic_count);   // error 
	used_pics = read("usedPictures.txt",usedPic_count); // error

	return 0;
}

string read(char* file, int& counter) {

	string array[299];
	ifstream infile;

	infile.open(file); 

	if (!infile) {
		cout << "Failed to open file";
		return 0;
	}

	while (infile) {
		getline(infile,array[counter],'\n');
		counter++;
	}

	return (array); // error
}

whats the exact error?

whats the exact error?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace std;

string read(char* file, int& counter);

int main() {

	int pic_count = 0;
	int usedPic_count = 0;
	string pic_array[299];
	string used_pics[299]


	pic_array = read("data.txt",pic_count);   // error  -incompatible types in assignment of `std::string' to `std::string[((unsigned int)((int)pic_count))]'

	used_pics = read("usedPictures.txt",usedPic_count); // error - incompatible types in assignment of `std::string' to `std::string[((unsigned int)((int)pic_count))]'


	return 0;
}

string read(char* file, int& counter) {

	string array[299];
	ifstream infile;

	infile.open(file); 

	if (!infile) {
		cout << "Failed to open file";
		return 0;
	}

	while (infile) {
		getline(infile,array[counter],'\n');
		counter++;
	}

	return (array); // error -conversion from `std::string*' to non-scalar type `std::string' requested
}

You are trying assign the int pic_count variable to a function that
returns string.

um, not really I am using pic_count to find out how many lines are in the text file, array is saving where the string/lines are being saved.

Anyone know how I can get rid of these errors, ie what I am doing wrong? :)

Oh, its getting late :

try this :

pic_array[0] = read("data.txt",pic_count);
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.