0

I am trying to take an inputted file that has two different sets of numbers and put them into two separate vectors. I'm not sure why I am unable to print out either vectors. Please help

Here is the file I'm trying to input and convert into two arrays (first section with 7 numbers is the ID's and the second section with 2 numbers are the test scores):

0773950 56
0773259 75
0732441 57
0772208 27
0775338 69
0763847 47
0771716 49
0773073 45
0774237 49
0775358 93
0763975 43
0779363 20
0775159 85
0777195 64
0764343 68
0773434 44
0766469 47
0777249 56
0777999 38
0777063 46
0774446 41
0710748 79
0771375 61
0774925 69
0774073 74
0773000 60
0763776 38
0776677 65
0763218 50
0773144 49
0773542 75
0774133 87

And here is the code I have so far:


#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

int main(){
    
    vector<int> id;
    vector<int> scores;
    
    int data1, data2;
    
    ifstream infile("Project.txt");
        
    if (!infile.is_open()){
        cout << "File Error" << endl;
        return 1;
    }
    
    while (infile.eof()){
        
        infile >> data1 && infile >> data2;
        
        id.push_back(data1);
        scores.push_back(data2);
        
    }
    
    infile.close();

    for (int i = 0; i < id.size(); i++){
        cout << "Here are the id's: " << id[i] << endl;
    }
    
    cout << "\n";
    
    for (int i = 0; i < scores.size(); i++){
        cout << "Here are the scores: " << scores[i] << endl;
    }
    
    
    return 0;
}
  • 3
    Fyi, [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – WhozCraig Feb 03 '22 at 23:23
  • What output did you get? – Vlad Feinstein Feb 03 '22 at 23:26
  • E.g, you were not only using `std::istream::eof` where you shouldn't, you weren't using the correct boolean condition on top of that. Concur with Tyler. Eliminate the first line inside the while loop, and make the while-expression as he shows. – WhozCraig Feb 03 '22 at 23:30
  • A small nitpick: you are using `std::endl` inside a loop but using `'\n'` in between. Logically it should be the other way around. – Dmitry Kuzminov Feb 03 '22 at 23:33

2 Answers2

2

You should not use

while (infile.eof())

as already pointed out in the comment section.

Just change it to:

while (infile >> data1 >> data2){
    id.push_back(data1);
    scores.push_back(data2);   
}

and it should work.

granmirupa
  • 2,780
  • 16
  • 27
  • Hey, thanks for the quick response, I really appreciate it! I fixed my while statement but my for loops are still not printing out what's in each vector. After I run the code I still just get "Program ended with exit code: 0". I feel like its probably a quick fix but I can't seem to figure it out. Thanks again! – user17544838 Feb 03 '22 at 23:45
  • @user17544838 You coud also read them into the same vector since the date (id and score) seems to be conected to eachother. [example](https://godbolt.org/z/czxnaPeb8) – Ted Lyngmo Feb 03 '22 at 23:47
  • @user17544838 I just tested the code and it works fine to me. Check whether you copied something wrongly. – granmirupa Feb 03 '22 at 23:52
2

This will correctly iterate until it hits the end of the file reading the two columns

while (infile >> data1 >> data2){
    id.push_back(data1);
    scores.push_back(data2);
}
Tyler V
  • 9,694
  • 3
  • 26
  • 52