0

I'm writing a small program where users can search national parks in America. I have all the parks with the states they're located in among other characteristics. Currently I only have it where a user will be shown all the parks along with the states they're in, the year they're established, and their website. However, I want the user to be able to search for parks by state, but I'm having trouble on figuring out how to search the text file for the state string and only displaying those parks. Here's what I have so far. `

#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
#include <fstream>

using namespace std;

int main()
{
    string parkname = "";
    string city = "";
    string state = "";
    string phone = "";
    string website = "";
    string famousfor = "";
    int yearestablished = 0;
    double dailyfee = 0;
    int sqyards = 0;
    char hikingtrails = ' ';
    char skiing = ' ';
    char wildernesscamp = ' ';
    char canoing = ' ';
    char kayaking = ' ';
    char guidedtours = ' ';
    char allseason = ' ';

    ifstream inFile;
    inFile.open("nationalparks.txt");
    if (inFile.is_open())
    {
        cout << "Park Name" << setw(25) << "City" << setw(31) << "State" << setw(41) << "Year Established" << setw(19) << "Website" << endl;
        getline(inFile, parkname, '#');
        getline(inFile, city, '#');
        getline(inFile, state, '#');
        inFile >> yearestablished;
        inFile.ignore();
        getline(inFile, phone, '#');
        getline(inFile, website, '#');
        inFile >> dailyfee;
        inFile.ignore();
        inFile >> sqyards;
        inFile.ignore();
        inFile >> hikingtrails;
        inFile.ignore();
        inFile >> skiing;
        inFile.ignore();
        inFile >> wildernesscamp;
        inFile.ignore();
        inFile >> canoing;
        inFile.ignore();
        inFile >> kayaking;
        inFile.ignore();
        inFile >> guidedtours;
        inFile.ignore();
        inFile >> allseason;
        inFile.ignore();
        getline(inFile, famousfor);
        while (!inFile.eof())
        {
            cout << parkname << setw(30) << city << setw(30) << state << setw(28) << yearestablished << setw(40) << website << setw(30) << endl;
            cout << left;
            getline(inFile, parkname, '#');
            getline(inFile, city, '#');
            getline(inFile, state, '#');
            inFile >> yearestablished;
            inFile.ignore();
            getline(inFile, phone, '#');
            getline(inFile, website, '#');
            inFile >> dailyfee;
            inFile.ignore();
            inFile >> sqyards;
            inFile.ignore();
            inFile >> hikingtrails;
            inFile.ignore();
            inFile >> skiing;
            inFile.ignore();
            inFile >> wildernesscamp;
            inFile.ignore();
            inFile >> canoing;
            inFile.ignore();
            inFile >> kayaking;
            inFile.ignore();
            inFile >> guidedtours;
            inFile.ignore();
            inFile >> allseason;
            inFile.ignore();
            getline(inFile, famousfor);

        } inFile.close();
    }
    system("pause");
    return 0;
}

I want the user to be able to enter for example alaska and only show those parks. I think a string search would work but im a little confused on how to do it.

JeJo
  • 20,530
  • 5
  • 29
  • 68
John0121
  • 1
  • 3
  • 2
    Before you can start on adding additional functionality, it's always a good idea to fix all your existing bugs, first. You can start with [the fact that `!infile.eof()` is a bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Sam Varshavchik Apr 30 '18 at 02:53
  • To search for something, it needs to be in a container. So add the records into a `std::vector`. Then look into `std::find_if()` – acraig5075 Apr 30 '18 at 06:26

0 Answers0