Advertisement
Sinux1

Final.cpp

May 19th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. //Create States structure
  8. struct States
  9. {
  10.     //Variable list that corresponds to input file format
  11.     string city;
  12.     string state;
  13.     string population;
  14. };
  15. //Function returns hard-coded filename
  16. void get_file_name(string &filename)
  17. {
  18.     filename = "pop.txt";
  19.     return;
  20. }
  21. //Function opens file
  22. void open_file(fstream &object_file, string name_of_file)
  23. {
  24.     object_file.open(name_of_file.c_str());
  25.     //Check for error loading file
  26.     if (!object_file)
  27.     {
  28.         cout << "Error loading file\n";
  29.         exit(0);
  30.     }
  31.     return;
  32. }
  33. //Function closes file
  34. void close_file(fstream &object_file)
  35. {
  36.     object_file.close();
  37.     return;
  38. }
  39. //Get user input
  40. string user_input()
  41. {
  42.     string user_input;
  43.     cout << "What state are you looking for?\n";
  44.     cin >> user_input;
  45.     return user_input;
  46. }
  47. //Search for and display cities in the state defined by user
  48. void display_cities(string input, fstream &input_file)
  49. {
  50.     string city, state, population;
  51.     States temp;
  52.         while (!input_file.eof())
  53.         {
  54.         getline(input_file, city);
  55.  
  56.         getline(input_file, state);
  57.  
  58.         getline(input_file, population);
  59.  
  60.         temp.city = city;
  61.         temp.state = state;
  62.         temp.population = population;
  63.  
  64.         if(temp.state == input)
  65.         {
  66.             cout << temp.city << endl;
  67.         }
  68.         }
  69.  
  70. }
  71. int main()
  72. {
  73.     string filename, state;
  74.     fstream data_store;
  75.     //Retrieve filename to work with
  76.     get_file_name(filename);
  77.     //Open the file
  78.     open_file(data_store, filename);
  79.     //Get input from user - name of state
  80.     state = user_input();
  81.     //Process file
  82.     display_cities(state, data_store);
  83.     //Close file
  84.     close_file(data_store);
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement