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;
}