иметь список переменных в цикле do-while, но в части while это приводит к ошибкам с «невозможно преобразовать» (void) 0 'в bool - PullRequest
0 голосов
/ 21 апреля 2019

У меня есть цикл do-while, использующий Do для заполнения нескольких векторов с помощью функции push_back (), но часть While со всеми переменными внутри возвращается с ошибкой «error: не удалось преобразовать» ((void ) 0, тип); из std :: string {aka std :: basic_string в bool

я попытался поставить "quoted (location)", но у меня появилась новая проблема: "quoted не объявленная функция" [я думал, что это могут быть мои функции компилятора в общем, я использую 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;
    }

Я ожидаю, что все это скомпилирует iss all. Я сортирую текстовый файл с данными с помощью этого кода.

РЕДАКТИРОВАТЬ :: я включил весь код, все функции и сортировку.

1 Ответ

1 голос
/ 21 апреля 2019

У вас есть две проблемы здесь:

  1. Не компилируется, потому что строки не конвертируются в bool. В выражении while (timeStamp, latitude, longitude, magnitude, depth, location, type) компилятор пытается преобразовать такие строки, как timeStamp в bool, что не удается. Попробуйте !timeStamp.empty() или другое логическое выражение.
  2. Ваше выражение while не выполняет то, что вы, вероятно, намереваетесь. Если вы пишете while(a, b), в условном выражении участвует только b. Вы, вероятно, хотите использовать &&, как while (a && b).
...