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;
}
現在、変数を設定するように設定しています。コードの短縮に役立つヘッダー ファイルがあります。最大人口を特定できるようにする必要がありますが、配列を使用せずにこれを行うにはどうすればよいでしょうか?