I have a do-while loop, using the Do to fill in multiple vectors with the push_back() function, but the While part with all the variables inside returns with an error of "error: could not convert '((void)0, type); from std::string {aka std::basic_string to bool

i tried putting "quoted(location)" but I get a new problem of "quoted is not a declared function" [I was thinking it might be my compiler functions in general, I use 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;
    }

I expect it all to compile iss all. I'm sorting a textfile full of data with this code.

EDIT:: i included the whole code, all functions and the sort.

1 Answers

1
Oz Solomon On

You have two problems here:

  1. It doesn't compile because strings are not convertible to bool. In the expression while (timeStamp, latitude, longitude, magnitude, depth, location, type) the compiler is trying to turn strings such as timeStamp to bool, which fails. Try !timeStamp.empty() or some other boolean expression.
  2. Your while expression isn't doing what you probably intend. If you write while(a, b), only b participates in the conditional. You probably want to use &&, like while (a && b).