为什么,即使使用for循环遍历我的向量,它也不会输出到控制台吗? (C ++)

问题描述 投票:0回答:1

这很混乱,因为我是C ++的新手。我做了一个函数,该函数返回向量的值,该向量从.txt文件中获取其元素。我制作了单独的向量对其进行排序,但这与该问题无关。

vector<string> txtToArr(int c) {
    ifstream unList;
    unList.open("unorderedList");
    vector<string> names;
    vector<double> times;
    vector<int> bNum;
    string tempS;
    double tempD;
    int tempI;

    // Adds data to their respective vectors
    int count = 0;
    while (!unList.eof()) {
        if (count == 1) {
            unList >> tempS;
            names.push_back(tempS);
        }
        else if (count == 2) {
            unList >> tempD;
            times.push_back(tempD);
        }
        else if (count == 3) {
            unList >> tempI;
            bNum.push_back(tempI);
            count = -1;
        }
        count++;
    }

    int small;
    string temp;
    double temp2;
    int temp3;

    // Sorts vectors using selection sort
    for (int i = 0; i < c - 1; i++) {
        small = i;
        for (int j = i + 1; j < c; j++) {
            if (times[j] < times[small]) {
                small = j;
            }
        }
        temp = names[i];
        temp2 = times[i];
        temp3 = bNum[i];
        times[i] = times[small];
        names[i] = names[small];
        bNum[i] = bNum[small];
        names[small] = temp;
        times[small] = temp2;
        bNum[small] = temp3;
    }

    // Compiles all of the vectors together
    vector<string> all;
    for (unsigned int i = 0; i < names.size(); i++) {
        all.push_back(to_string(i+1));
        all.push_back(names[i]);
        all.push_back(to_string(times[i]));
        all.push_back(to_string(bNum[i]));
    }

    unList.close();
    return all;
}


然后我尝试使用以下命令将其输出到控制台:

        // Outputs vectors (This is in the main btw)
        int count2 = 0;
        for (unsigned int i = 0; i < txtToArr(count).size(); i++) {
            if (count2 == 3) {
                cout << "\n";
                count2 = -1;
            }
            cout << txtToArr(count).at(i) << " ";
            count2++;
        }

但是由于某些原因,它不会输出到控制台。我敢肯定这是我犯的一些小错误,但似乎找不到。

感谢您的帮助!

这里是完整的代码,如果有帮助的话:

// This program takes your name and your fastest 5k(running) times. It puts that data in a .txt file, and reads that data.
// It then outputs a ranking compared to other people entered in and displays a randomly generated bib or racing number.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

// Generates random bib number between 1 and 100
int bibNum() {
    return rand() % 100 + 1;
}

vector<string> txtToArr(int c) {
    ifstream unList;
    unList.open("unorderedList");
    vector<string> names;
    vector<double> times;
    vector<int> bNum;
    string tempS;
    double tempD;
    int tempI;

    // Adds data to their respective vectors
    int count = 0;
    while (!unList.eof()) {
        if (count == 1) {
            unList >> tempS;
            names.push_back(tempS);
        }
        else if (count == 2) {
            unList >> tempD;
            times.push_back(tempD);
        }
        else if (count == 3) {
            unList >> tempI;
            bNum.push_back(tempI);
            count = -1;
        }
        count++;
    }

    int small;
    string temp;
    double temp2;
    int temp3;

    // Sorts vectors using selection sort
    for (int i = 0; i < c - 1; i++) {
        small = i;
        for (int j = i + 1; j < c; j++) {
            if (times[j] < times[small]) {
                small = j;
            }
        }
        temp = names[i];
        temp2 = times[i];
        temp3 = bNum[i];
        times[i] = times[small];
        names[i] = names[small];
        bNum[i] = bNum[small];
        names[small] = temp;
        times[small] = temp2;
        bNum[small] = temp3;
    }

    // Compiles all of the vectors together
    vector<string> all;
    for (unsigned int i = 0; i < names.size(); i++) {
        all.push_back(to_string(i+1));
        all.push_back(names[i]);
        all.push_back(to_string(times[i]));
        all.push_back(to_string(bNum[i]));
    }

    unList.close();
    return all;
}

int main()
{
    // Only prints out 2 digits after the decimal
    cout << setprecision(2) << fixed;

    // Variables
    string name;
    double PR;
    int input;
    int count = 0;
    ofstream unorderedList;
    unorderedList.open("unorderedList.txt");

    cout << "This program takes your name and your fastest 5k(running) times.\nIt puts that data in a .txt file, and reads that\ndata. It then outputs a ranking compared to other people entered\nin and displays a randomly generated bib or racing number.\n\n";
    do {
        count++;
        unorderedList << count << " ";
        cout << "Enter your name: ";
        cin >> name;
        unorderedList << name << " ";

        cout << "\n\nEnter in your fastest 5k. For example 17:41 = 17.41: ";
        cin >> PR;
        unorderedList << PR << " " << bibNum() << "\n";
        cout << "1 to continue entering times / 2 to see the data";
        cin >> input;


    } while (input == 1);

    if (input == 2) {
        // Outputs vectors
        int count2 = 0;
        for (unsigned int i = 0; i < txtToArr(count).size(); i++) {
            if (count2 == 3) {
                cout << "\n";
                count2 = -1;
            }
            cout << txtToArr(count).at(i) << " ";
            count2++;
        }
        exit(0);
    }

}
c++ c++11 vector io output
1个回答
0
投票

首先要检查的是:“ unorderedList.txt”甚至没有任何数据。可以使用文本编辑器轻松检查。不,不是这样,因为您在完成数据写入后忘了关闭它。

© www.soinside.com 2019 - 2024. All rights reserved.