我正在研究分析具有未知行數的輸入文件的代碼。每行的格式爲「國家,城市,城市,州,人口,經度,緯度」。我目前在我的代碼設置最小和最大的人羣時出錯。錯誤說「左操作數必須是左值」。我試圖尋找這個,但無法找到答案。編寫程序來分析文件
#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