0

This is messy because I am new to c++. I made a function that returns the value of a vector getting it's elements from a .txt file. I made seperate vectors to sort it but that is not relevant to this question.

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;
}


I then tried outputting it to the console using the following:

        // 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++;
        }

But for some reason it will not output to the console. I'm sure it's some minor mistake I made but I can't seem to find it.

Thanks for your help!

Here is the full code if it helps:

// 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);
    }

}
CPD3408
  • 1
  • 2
  • In your full code you open unList each time you run txtToArr, but in your original code you did not. Which is correct? – gmatht Jun 17 '20 at 03:41
  • Please see [How to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – jdigital Jun 17 '20 at 03:43
  • @gmatht - The full code is correct. I open and close the ifstream object 'unList' each time I run txtToArr(). – CPD3408 Jun 17 '20 at 03:46

2 Answers2

0

The first thing to check is: does "unorderedList.txt" even have any data. This can be easily checked using a text editor. No it does not, because you forgot to close it after finishing writing your data.

Also the following loop reads the file every loop.

for (unsigned int i = 0; i < txtToArr(count).size(); i++) {

That is odd. Consider storing it into a variable, and reusing that. E.g.

vector<string> data = txtToArr(count);
for (unsigned int i = 0; i < data.size(); i++) {
gmatht
  • 835
  • 6
  • 14
  • I ended up adding it after the do while loop (in the main). I see the information in the .txt file but not when I try to print it out still. – CPD3408 Jun 17 '20 at 03:55
  • I changed that too but that still didn't work. I just noticed something when I run it and it gets to this specific for loop it seems to do an infinate loop because after the for loop I have "exit(0)" but it never indicates that it exits. Do you see what could be causing this? – CPD3408 Jun 17 '20 at 04:06
  • I am currently getting an infinite loop at `while (!unList.eof()) {`. – gmatht Jun 17 '20 at 04:11
  • I changed the while loop to "while(unList.good())" I'm currently looking into what I think is a bounds error but I cannot seem to find it. it says "Vector subscript out of range." I'm not sure if this has to do with the while loop or not. Let me know if you find anything. – CPD3408 Jun 17 '20 at 04:53
  • I just figured out the bounds error. There were actually two of them, one at the sort, and one at the adding of elements to the vector "all." But my problem seems to persist with nothing printing to the console. – CPD3408 Jun 17 '20 at 12:34
  • I ended up figuring it out. The if statements in the while loop were causing nothing to be entered into each vector leading to nothing being outputed. So, I deleted those if statements and it fixed it. Thanks for your help! – CPD3408 Jun 17 '20 at 14:02
0
  1. When you set the input file you need to add the .txt extension, so "unorderedList.txt"

  2. Close the output file before opening the input file, this can be done using unorderedList.close()

  3. I would strongly advise you to change how you are reading file input as you are currently infinitely looping as the while (!unList.eof()) loop is not terminating. I would advise following the tutorial at this link: http://www.cplusplus.com/doc/tutorial/files/ or using C style get line, the following link contains a great example of how to do this: C read file line by line

Hope this helps.

jnkarate
  • 46
  • 5
  • Yes that did help alot. I fixed all of those things and I changed the while loop to "while(unList.good())" I'm currently looking into what I think is a bounds error but I cannot seem to find it. it says "Vector subscript out of range." I'm not sure if this has to do with the while loop or not. Let me know if you find anything. – CPD3408 Jun 17 '20 at 04:49
  • I just figured out the bounds error. There were actually two of them, one at the sort, and one at the adding of elements to the vector "all." But my problem seems to persist with nothing printing to the console. – CPD3408 Jun 17 '20 at 12:34
  • I ended up figuring it out. The if statements in the while loop were causing nothing to be entered into each vector leading to nothing being outputed. So, I deleted those if statements and it fixed it. Thanks for your help! – CPD3408 Jun 17 '20 at 14:02