我正在编写一个分析行数未知的输入文件的代码。每行的格式为“国家、城市、城市、州、人口、纬度、经度”。我目前遇到一个错误,我的代码设置了最小和最大的人口。错误说“左操作数必须是左值”。我尝试查找此内容,但找不到答案。
#include "city.h"
#include <iostream>
#include <fstream>
#include <string>
using std::string;
using std::ifstream;
using std::istream;
using std::ostream;
using std::cout;
using std::endl;
using std::getline;
void readLineOfData( istream& in, ostream& out, 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( istream& in, ostream& out, string country, string city, string city2,
string state, int pop, string lat, string longi )
{
int smallestPop = 0;
int largestPop = 0;
string smallestCity;
string largestCity;
cout << country << endl;
cout << city << endl;
cout << city2 << endl;
cout << state << endl;
cout << pop << endl;
cout << lat << endl;
cout << longi << endl;
if (pop < smallestPop || smallestPop == 0)
{
smallestPop = pop;
smallestCity = city;
}
if (pop > largestPop || largestPop == 0)
{
largestPop = pop;
largestCity = city;
}
out << "Smallest City: " << smallestCity << endl;
out << "Population: " << smallestPop << endl;
out << endl;
out << "Largest City: " << largestCity << endl;
out << "Largest Population: " << largestPop << endl;
}
任何帮助,将不胜感激。