我正在编写一个小程序,用户可以搜索美国的国家公园。我所有的公园都有他们所处的州以及其他特征。目前我只有一个用户将会看到所有公园以及他们所在的州,他们建立的年份以及他们的网站。但是,我希望用户能够按州搜索公园,但我无法确定如何在文本文件中搜索状态字符串并仅显示这些公园。这是我到目前为止所拥有的。 `
#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;
}
我希望用户能够输入例如alaska
并仅显示这些公园。我认为字符串搜索可以工作,但我对如何做它有点困惑。