I'm wiring a program where I have a file with city, state, population size laid out as follows;
24
El Paso, TX 577415
Indianapolis, IN 783612
Detroit, MI 925051
Fort Worth, TX 567516
Los Angeles, CA 3798981
Washington, DC 570898
etc... with 24 being the number of elements in the file.

I'm trying to write the data in the file into character arrays, char city[], char state[], int size[]

however I am having a problem with the getline command. I was originally using strings to store the data and had no problems, but I also need to use strcmp to alphabetize the data by city name and I was getting an error with strcmp not liking the strings.

int main()
{
	const int LIMIT = 50;
	string ifile,ofile;
	char city[LIMIT], state[LIMIT],;
    int size[LIMIT],num, menu;
	ofstream fout;
	ifstream fin;

	cout << "Enter the input file name: ";
	cin >> ifile;
	fin.open(ifile.c_str());
	if (fin.fail())
	{
		cout << "Open failed\n";
		exit(1);
	}

	cout.setf(ios::left);
	fout.setf(ios::left);
	fin >> num;
	fin.ignore(1000, '\n');
	if (num > LIMIT)
	{
		cout << "Too many lines of data\n";
		exit(1);
	}
	for (int i = 0; i< num; i++)
	{
		getline(fin,city[i],',');
		fin >> state[i] >> size[i];
		fin.ignore(1000, '\n');
    }

It is a lot easier to sort the data if you should create a structure to hold the information for one city then create an array of those structures.

>>strcmp not liking the strings.
Yes, strcmp() wants char*, not strings. If you using string objects then you can pass method c_str() to strcmp().

I feel like a huge idiot. I tried the c_str() method earlier, however I had it written wrong.

I was putting it like:
strcmp (city.c_str(j), city.c_str(j+1));
instead of;
strcmp (city[j].c_str(), city[j+1].c_str());

For some reason it hit me as soon as I read your reply. Thanks for the response!

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.