Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <cstdlib>
- using namespace std;
- //Create States structure
- struct States
- {
- //Variable list that corresponds to input file format
- string city;
- string state;
- string population;
- };
- //Function returns hard-coded filename
- void get_file_name(string &filename)
- {
- filename = "pop.txt";
- return;
- }
- //Function opens file
- void open_file(fstream &object_file, string name_of_file)
- {
- object_file.open(name_of_file.c_str());
- //Check for error loading file
- if (!object_file)
- {
- cout << "Error loading file\n";
- exit(0);
- }
- return;
- }
- //Function closes file
- void close_file(fstream &object_file)
- {
- object_file.close();
- return;
- }
- //Get user input
- string user_input()
- {
- string user_input;
- cout << "What state are you looking for?\n";
- cin >> user_input;
- return user_input;
- }
- //Search for and display cities in the state defined by user
- void display_cities(string input, fstream &input_file)
- {
- string city, state, population;
- States temp;
- while (!input_file.eof())
- {
- getline(input_file, city);
- getline(input_file, state);
- getline(input_file, population);
- temp.city = city;
- temp.state = state;
- temp.population = population;
- if(temp.state == input)
- {
- cout << temp.city << endl;
- }
- }
- }
- int main()
- {
- string filename, state;
- fstream data_store;
- //Retrieve filename to work with
- get_file_name(filename);
- //Open the file
- open_file(data_store, filename);
- //Get input from user - name of state
- state = user_input();
- //Process file
- display_cities(state, data_store);
- //Close file
- close_file(data_store);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement