-2

I am currently trying to make a learning Ai from scratch to familiarize myself with how it all works (project code is linked here My current issue is finding a way to stop the while loop on line 20 once the eof has been reached. I have tried using fin.eof, the output I get when I use this is "Segmentation fault (core dumped)". how can I get the code to run properly?

Here is my code:

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

struct Match{
    vector<int> sitVal;
    vector<int> opVal1;
    vector<int> opVal2;
    vector<int> opVal3;
};
//fills chosen Match with data from file
void conData(string file, Match& sits){
    ifstream fin;
    fin.open(file);
    int jump;
    fin >> jump;
    int i = 0;
    while(true){
        fin >> sits.sitVal[i];
        if (!fin)
            break;
        fin >> sits.opVal1[i];
        fin >> sits.opVal2[i];
        fin >> sits.opVal3[i];
        i++;
    }
    //following is for testing
    cout << sits.sitVal[0];
    cout << sits.opVal1[0];
    cout << sits.opVal2[0];
    cout << sits.opVal3[0];
}

int main(){
    Match sits;
    conData("Data.txt", sits);
}
drescherjm
  • 8,907
  • 5
  • 42
  • 60
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – πάντα ῥεῖ Mar 22 '21 at 13:49
  • please provide a [mre] within the question without relying on external links – Alan Birtles Mar 22 '21 at 13:50
  • Why not `std::vector>` instead of 4 seemingly unrelated vectors? – tadman Mar 22 '21 at 13:52
  • 3
    your vectors are all empty so `sits.sitVal[i]` etc. have undefined behaviour – Alan Birtles Mar 22 '21 at 13:52
  • 1
    @tadman I think `struct rowData { int sitVal, opVal1, opVal2, opVal3; };` and `std::vector` is better. – MikeCAT Mar 22 '21 at 13:53
  • 2
    `fin >> sits.sitVal[i];` -- Change that to `fin >> sits.sitVal.at(i);`, and then you will discover the reason for the seg faults. – PaulMcKenzie Mar 22 '21 at 13:54
  • `fin >> sits.sitVal[i];` is undefined behavior, due to `sits` being empty. In fact, you would only need the following example: `int main () {std::vector sitVal; sitVal[0];}` to produce segfault for the same reason. Which is why we ask to provide [mre], and not the entire codebase. – Algirdas Preidžius Mar 22 '21 at 13:57
  • @AlgirdasPreidžius [Your example](https://wandbox.org/permlink/DxUdiPfamJNdXcOL) may not be sufficient to produce segfault because the value `sitVal[i]` is not used. Undefined behavior is undefined. – MikeCAT Mar 22 '21 at 13:58
  • @MikeCAT Getting better, yeah, but [Zero, One or Infinity](https://en.wikipedia.org/wiki/Zero_one_infinity_rule) applies here. Is that really best as `opValN` or would `opVal[3]` be a better fit? – tadman Mar 22 '21 at 14:18

1 Answers1

0

You must add elements where the data is read to before writing data read.

    while(true){
        // read first data
        int sitValTemp;
        fin >> sitValTemp;
        if (!fin)
            break;

        // add elements
        sits.sitVal.push_back(sitValTemp);
        sits.opVal1.push_back(0);
        sits.opVal2.push_back(0);
        sits.opVal3.push_back(0);

        // read rest data
        fin >> sits.opVal1[i];
        fin >> sits.opVal2[i];
        fin >> sits.opVal3[i];
        i++;
    }
MikeCAT
  • 61,086
  • 10
  • 41
  • 58