2013-03-09 106 views
-1

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

#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; 

} 

任何幫助,將不勝感激。

+1

如果您對這兩行發表了這條錯誤的評論,現在您會有多個答案。一旦突出顯示線條與之前相比突出顯示就非常容易了。這兩個是'if(pop largestPop || largestPop = 0)'。另外,只要把它扔到那裏,GCC 4.8.0會在單個等號下面加上一個插入符號,而Clang 3.2也會這樣做,但強調了它之前的所有內容,並在其之前加上撇號。發現錯誤有多酷? – chris 2013-03-09 03:38:47

+0

問題在哪裏? – Benjamin 2013-03-09 05:52:45

+0

您應該保留回答時的問題 – dreamcrash 2013-03-09 06:09:25

回答

2

你在幾個表達式中使用=代替==

if (pop < smallestPop || smallestPop = 0) 

和:

if (pop > largestPop || largestPop = 0) 

所以你在做一個任務,而不是一個比較。由於operator precedence,我們看到此錯誤。由於兩個<||在第一種情況下具有更高的優先級比=我們最終有:

((pop > smallestPop) || smallestPop) = 0 

這是分配給一個lvalue。如果另一方面,你有這樣的:

if (pop < smallestPop || (smallestPop = 0)) 

該程序將編譯得很好,因爲括號會導致首先發生的任務。在評論中提到的避免這些類型問題的簡單方法是將常數放在左邊。雖然我喜歡這個解決方案,但很多開發人員都會忽視這種非傳統的符號。

+0

謝謝,這是錯誤。 – user2145500 2013-03-09 03:43:03

+0

爲了避免這個錯誤,把常量放在左邊,它給出一個錯誤,0 = smallestPop是一個錯誤,編譯器會告訴你。 – QuentinUK 2013-03-09 03:52:24

0

快速猜測,從看代碼。我已經看到一種常見的情況,那就是報告「左操作數必須是左值」,當您在嘗試比較相等性時意外使用「=」(賦值)運算符(C++和許多其他語言中的「==」)時, 。