gpt4 book ai didi

c++ - 在 do-while 循环中有一个变量列表,但在 while 部分它会出错, "cannot convert ' (void)0' to bool

转载 作者:行者123 更新时间:2023-11-28 04:19:35 25 4
gpt4 key购买 nike

我有一个 do-while 循环,使用 Do 用 push_back() 函数填充多个 vector ,但是带有所有变量的 While 部分返回错误“error: could not convert '((void )0, 类型); 从 std::string {aka std::basic_string 到 bool

我尝试放置“quoted(location)”但我遇到了“quoted is not a declared function”的新问题[我想这可能是我的编译器函数,我使用的是 cygwin64]

    #include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

double avg(const vector<double>& v){
int sum = 0;
for (int i = 0; i < v.size(); i++)
sum+= v.at(i);

return (1.0 * sum / v.size());
}

double max(const vector<double>& v) {
int maxelt = v.at(0);
for (int i = 1; i < v.size(); ++i) {
if (v.at(i) < maxelt)
maxelt = v.at(i);
}
return (maxelt);
}

void selection_sort(vector<double>&a, vector<double>& b) {
int max;
for(int i = 0; i < a.size()-1; i++) {
max = i;
for (int j = i + 1; j < a.size(); j++)
if((a.at(j) > a.at(max)) || ((a.at(j) == a.at(max)) &&
(b.at(j) < b.at(max)))
)
max = j;
swap(a.at(i), a.at(max));
swap(b.at(i), b.at(max));
}
}


void print_usage();

int main(int argc, char *argv[]) {
if (argc != 2) {
print_usage();
}

string junkline;
unsigned int i;
vector<string> timeStamps;
vector<double> latitudes;
vector<double> longitudes;
vector<double> depths;
vector<double> magnitudes;
vector<string> locations;
vector<string> types;

double avgMagnitude;
double avgDepth;
string timeStamp;
double latitude;
double longitude;
double magnitude;
double depth;
string location;
string type;

getline(cin, junkline);

do {
getline(cin, timeStamp);
timeStamps.push_back(timeStamp);
cin >> latitude;
latitudes.push_back(latitude);
cin >> longitude;
longitudes.push_back(longitude);
cin >> magnitude;
magnitudes.push_back(magnitude);
cin >> depth;
depths.push_back(depth);
getline(cin, quoted(location));
locations.push_back(location);
getline(cin, type);
types.push_back(type);
}
while (timeStamp, latitude, longitude, magnitude, depth, location,
type);

avgMagnitude = avg(magnitudes);
avgDepth = avg(depths);

cout << "The average magnitude: " << avgMagnitude << endl;
cout << "The average depth: " << avgDepth << endl;

selection_sort(longitudes, latitudes);



ofstream new_all_month;
new_all_month.open ("sorted data.csv");
new_all_month << "Timestamp, latitude, longitude, magnitude,
depth, location, type" << endl;
for (i = 0; i < timeStamps.size(); i++) {
new_all_month << timeStamps.at(i) << "," << latitudes.at(i) <<
"," << longitudes.at(i) << "," << magnitudes.at(i) << "," <<
depths.at(i) << "," << locations.at(i) << "," << types.at(i) << endl;
}
new_all_month.close();


return 0;
}

我希望这一切都能编译成功。我正在使用这段代码对充满数据的文本文件进行排序。

编辑::我包括了整个代码、所有函数和排序。

最佳答案

这里有两个问题:

  1. 它无法编译,因为字符串不能转换为 bool。在表达式 while (timeStamp, latitude, longitude, magnitude, depth, location, type) 中,编译器试图将 timeStamp 等字符串转换为 bool,失败。尝试 !timeStamp.empty() 或其他一些 bool 表达式。
  2. 您的 while 表达式没有按照您的预期进行。如果你写while(a, b),只有b参与条件。您可能想使用 &&,例如 while (a && b)

关于c++ - 在 do-while 循环中有一个变量列表,但在 while 部分它会出错, "cannot convert ' (void)0' to bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55779533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com