2013-03-09 100 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++和许多其他语言中的“==”)时, 。