C++ 编写分析文件的程序
我正在编写一个代码,用于分析具有未知行数的输入文件。每行的格式为“国家、城市、城市、州、人口、纬度、经度”。我目前遇到一个错误,我的代码设置了最小和最大的群体。错误表示“左操作数必须是左值”。我试着查找这个问题,但找不到答案C++ 编写分析文件的程序,c++,C++,我正在编写一个代码,用于分析具有未知行数的输入文件。每行的格式为“国家、城市、城市、州、人口、纬度、经度”。我目前遇到一个错误,我的代码设置了最小和最大的群体。错误表示“左操作数必须是左值”。我试着查找这个问题,但找不到答案 #include "city.h" #include <iostream> #include <fstream> #include <string> using std::string; using std::ifstream; usi
#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;
}
#包括“city.h”
#包括
#包括
#包括
使用std::string;
使用std::ifstream;
使用std::istream;
使用std::ostream;
使用std::cout;
使用std::endl;
使用std::getline;
无效的读取线数据(istream&in、ostream&out、string&country、string&city、string&city2、,
字符串和状态、int和pop、字符串和lat、字符串和longi);
无效输出(ostream&out、字符串国家/地区、字符串城市、字符串城市2、,
字符串状态、int-pop、字符串lat、字符串longi);
虚空城市(istream和in、ostream和out)
{
ifstream(“cities.txt”);
字符串国家、城市、城市2、州、拉特、朗吉;
int-pop;
readLineOfData(in、country、city、city2、state、pop、lat、longi);
而(!in.fail())
{
输出(国家、城市、城市2、州、pop、lat、longi);
readLineOfData(in、country、city、city2、state、pop、lat、longi);
}
返回;
}
无效readLineOfData(istream&in、string&country、string&city、string&city2、,
字符串和状态、int和pop、字符串和lat、字符串和longi)
{
getline(在,国家,“,”);
getline(在,城市,,);
getline(在,城市2,',');
getline(in,state,,);
在>>流行音乐中;
忽略(200,,);
getline(in,lat,,);
getline(in,longi,'\n');
}
无效输出(istream&in、ostream&out、字符串国家/地区、字符串城市、字符串城市2、,
字符串状态、int-pop、字符串lat、字符串longi)
{
int smallestPop=0;
int largestPop=0;
弦小城市;
弦最大度;
CUT< P>快速查看,从代码中看到。我看到的一个常见情况,报告“左操作数必须是一个LValk”,是当你尝试比较相等(“C++”和其他许多语言中的“==”)时意外使用“=”(赋值)运算符。.您在几个表达式中使用的是=
而不是=
:
if (pop < smallestPop || smallestPop = 0)
所以你是在做作业,而不是比较。我们看到这个错误是由于。因为如果你在两行上都加了注释,就会出现这个错误,那么现在你已经有多个答案了。与以前相比,高亮显示这行后很容易发现。这两行是If(pop
和if(pop>largestPop | | largestPop=0)
。另外,GCC 4.8.0在单等号下加了一个插入符号,而Clang 3.2也做了同样的事情,但在它前面的if条件下用波浪线强调所有内容。这对于发现错误有多酷?问题在哪里?你应该保持回答问题时的状态Hanks,这就是错误。为了避免他的错误将常量放在左边,它给出了一个错误,0=smallestPop是一个错误,编译器会告诉您。
if (pop > largestPop || largestPop = 0)
((pop > smallestPop) || smallestPop) = 0
if ( pop < smallestPop || (smallestPop = 0) )