首页 > 解决方案 > 在 do-while 循环中有一个变量列表,但在 while 部分它错误“无法将 '(void)0' 转换为 bool

问题描述

我有一个 do-while 循环,使用 Do 用 push_back() 函数填充多个向量,但是内部所有变量的 While 部分返回错误“错误:无法转换'((void)0, type); 从 std::string {aka std::basic_string 到 bool

我尝试输入“引用(位置)”,但我遇到了一个新问题“引用不是声明的函数”[我想这可能是我的编译器函数,我使用 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;
    }

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

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

标签: c++c++14

解决方案


你在这里有两个问题:

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

推荐阅读