I'm having so much trouble with this assignment, and I was wondering if anyone would be able to help me out. Here is the assignment:

This program uses the file "cities.txt", which contains over 136,000 location names in the United States. Each line of the file contains a place name and state. The last three characters of each line should be a space and the two-letter state abbreviation.

Write a program that will read through each line of this file. Find the longest names in the list. Print to the screen the names and states of the three longest place names in the file. You should #include <string> and use the string object and its .length() function for this assignment.

The code that I have is not complete. I'm having trouble with the .length() function and am also getting a getch() error for some reason that I can't figure out...

The program is based off of a previous assignment, using the same file (cities.txt). the string bigstring and substring are used to seach through the cities in the file...but I'm not sure that I even have that part right *sigh*

This is what I have so far:

#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;

int main()
{
	ifstream file;
	string bigstring, substring, Longest, SecondLongest, ThirdLongest;
	file.open("c:\\cities.txt");

	while (getline(file,bigstring)) // Read a line. If successful, enter the loop.
    {
		bigstring = substring; 
		if (bigstring.find(substring) != -1) // Returns the position of substring within bigstring. If substring doesn't exist in bigstring, -1 is returned.
		{
			cout << substring << endl;
		}
        
    }

	int length = Longest.length();
	cout << "The longest city name in the file is: " << Longest << endl;

	cout << "The second longest city name in the file is: " << SecondLongest << endl;

	cout << "The third longest city name in the file is: " << ThirdLongest << endl;

	getch();
	return 0;
}

Why are you using getch() there? So the console doesn't close? Your compiler must support it. Use std::cin.get() instead.

Would be good if you gave and example of how the file is formatted exactly so it will be easier to help.

What is wrong with length()? length() is an alias of size(). They both do exactly the same thing so try size() instead and se what happens, both should give you the number of characters in the string. You should also use size_t as the type for the length variable and not an int, but I think it will work anyways.

you are not defining a value for substring, so it won't ever be found in the bigstring line

nor are values defined for longest, secondlongest or third longest

also you should use cin.get() instead of getch()

i don't really see the purpose or understand the usage of the substring variable to comment on it,

but what i would do for finding out the maximum lengths is create three variables and compare them to each other and to bigstring.length() when you are reading it into your variable

I'm using getch() there because that would be the end of the program. That's how my teacher always said to use it. The file is formatted as a notepad file with the city name and two letter state abbreviation. He wants us to sue length() like that, but I'm not sure how I'd use it for the second and third longest...the same way?

My teacher told us to use bigstring and substring to search through the file.
For example, the city Springfield, bigstring would be Springfield, while substring would be Spring. Spring Lake: bigstring = Spring Lake, substring = Spring.

Well either you misunderstood your teacher about getch(), or your teacher has lacking skills in C++, which is fine. But use cin.get() instead, it will do the same thing, and it has nothing to do with the program ending. It is used so that the console doesn't close in windows when the program has ended.

This article has been dead for over six months. Start a new discussion instead.