我需要 C++ 从输入文件中获取信息并将其存储为不同变量的帮助。它采用以下格式。
我们,诺斯菲尔德,诺斯菲尔德,弗吉尼亚州,9342,38.8042,-77.205
我该怎么做呢?
编辑:对不起,这是我第一次使用论坛。这就是我到目前为止所拥有的。
#include "city.h"
void readLineOfData( istream& in, string &country, string &city, string &city2,
string &state, int &pop, string &lat, string &longi);
void output( ostream& out, string country, string city, string city2,
string state, int pop, string lat, string longi );
void cities( istream& in, ostream& out )
{
ifstream ("cities.txt");
string country, city, city2, state, lat, longi;
int pop;
readLineOfData(in, country, city, city2, state, pop, lat, longi);
while(!in.fail())
{
output( cout, country, city, city2, state, pop, lat, longi );
readLineOfData(in, country, city, city2, state, pop, lat, longi);
}
return;
}
void readLineOfData( istream& in, string &country, string &city, string &city2,
string &state, int &pop, string &lat, string &longi)
{
getline( in, country, ',');
getline( in, city, ',');
getline( in, city2, ',');
getline( in, state, ',');
in >> pop;
in.ignore( 200, ',' );
getline( in, lat, ',');
getline( in, longi, '\n' );
}
void output( ostream& out, string country, string city, string city2,
string state, int pop, string lat, string longi )
{
out << country << endl;
out << city << endl;
out << city2 << endl;
out << state << endl;
out << pop << endl;
out << lat << endl;
out << longi << endl;
}
目前我已经设置它来设置变量。我有一个有助于缩短代码的头文件。我现在需要能够识别最高的人口,如果不使用数组,我将如何做到这一点?