0

I'm trying to populate the array of structure with some data inside a text file. When I run the code, it directed me to the xmemory thing, shows me some warning, and the program stuck. I have attached the warning message below the code.

There are no red lines shown before I run.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

struct date {
    int day;
    int month;
    int year;
};

struct tutorRecord {
    string TutorID;
    string Name;
    date DateJ;
    date DateT;
    string payrate;
    string Phone;
    string address;
    string TCcode;
    string TCname;
    string Scode;
    string Sname;
    int rating;
    tutorRecord() {}
    tutorRecord(string id, string name, int dayjoined, int monthjoined, int yearjoined, int dayterminated, int monthterminated, int yearterminated, 
        string rate, string phone, string tcode, string tname, string ads, string scode, string sname, int star) {
        TutorID = id;
        Name = name;
        DateJ.day = dayjoined;
        DateJ.month = monthjoined;
        DateJ.year = yearjoined;
        DateT.day = dayterminated;
        DateT.month = monthterminated;
        DateT.year = yearterminated;
        payrate = rate;
        Phone = phone;
        TCcode = tcode;
        TCname = tname;
        address = ads;
        Scode = scode;
        Sname = name;
        rating = star;
    }
};

class fileio {
private:

public:
    int scan() {
        ifstream myfile;
        string line;
        int count = 0;
        myfile.open("tutorrecord.txt");
        while (getline(myfile, line)) {
            count++;
        }
        return count;
    }
};

void getlist();

int main() {
    getlist();
    return 0;
}

void getlist() {
    string line;
    int tut = 0;
    fileio filing;
    ifstream file;
    int size = filing.scan();
    tutorRecord* list = new tutorRecord[size];
    file.open("tutorlist.txt");
    while (getline(file, line)) {
        stringstream ss(line);
        string tutor[16];
        string thing;
        int i = 0;
        while(getline(ss, thing, ';')){
            tutor[i] = thing;
            i++;
        }

        int jday = stoi(tutor[2]);
        int jmonth = stoi(tutor[3]);
        int jyear = stoi(tutor[4]);
        int tday = stoi(tutor[5]);
        int tmonth = stoi(tutor[6]);
        int tyear = stoi(tutor[7]);
        int star = stoi(tutor[15]);
        list[tut] = tutorRecord(tutor[0], tutor[1], jday, jmonth, jyear, tday, tmonth, tyear, tutor[8],
            tutor[9], tutor[10], tutor[11], tutor[12], tutor[13], tutor[14], star);
        cout << list[tut].Name;
        tut++;
    }
    file.close();
}

Inside text file

202;Richard;24;3;2020;24;3;2021;200;029181;D2;eXcel Domino 2;Domino;C;Chemistry;5 32;Hong;26;3;2020;26;3;2021;213;0219321;P2;eXcel Prague 2;Prague;C;Chemistry;5

  • Severity Code Description Project File Line Suppression State Warning C26495 Variable 'tutorRecord::DateJ' is uninitialized. Always initialize a member variable (type.6).
  • Severity Code Description Project File Line Suppression State Warning C26495 Variable 'tutorRecord::DateT' is uninitialized. Always initialize a member variable (type.6).
  • Severity Code Description Project File Line Suppression State Warning C26495 Variable 'tutorRecord::rating' is uninitialized. Always initialize a member variable (type.6).
  • Severity Code Description Project File Line Suppression State Warning C6385 Reading invalid data from 'list': the readable size is '(unsigned int)*280+4' bytes, but '560' bytes may be read.
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • Please [edit] your question to include the *full* and *complete* error output, copy-pasted without any modifications or editing. Also please mark out the lines where you get the errors by adding comments on those lines. – Some programmer dude Apr 15 '20 at 10:53
  • On another couple of notes, I recommend that you learn about *constructor initializer lists*, use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead of your own dynamically allocated arrays, and read [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Apr 15 '20 at 10:54
  • Also, unless this is for a school or book exercise or assignment, then please don't attempt to parse CSV files yourself. There are many good libraries which can handle arbitrary CSV files much better than your program can. – Some programmer dude Apr 15 '20 at 12:46

0 Answers0