我正在研究分析具有未知行数的输入文件的代码。每行的格式为“国家,城市,城市,州,人口,经度,纬度”。我目前在我的代码设置最小和最大的人群时出错。错误说“左操作数必须是左值”。我试图寻找这个,但无法找到答案。编写程序来分析文件
#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;
}
任何帮助,将不胜感激。
如果您对这两行发表了这条错误的评论,现在您会有多个答案。一旦突出显示线条与之前相比突出显示就非常容易了。这两个是'if(pop largestPop || largestPop = 0)'。另外,只要把它扔到那里,GCC 4.8.0会在单个等号下面加上一个插入符号,而Clang 3.2也会这样做,但强调了它之前的所有内容,并在其之前加上撇号。发现错误有多酷? –
chris
2013-03-09 03:38:47
问题在哪里? – Benjamin 2013-03-09 05:52:45
您应该保留回答时的问题 – dreamcrash 2013-03-09 06:09:25