So this here is my header file "Header.h" in which my struct is made named info:
#pragma once
#include<iostream>
#include<string>
using namespace std;
struct info {
string name;
int num;
info();
info(string names, int numbers);
info* next;
};
typedef info* ptr;
Here is my info.cpp file in which defines the constructor and includes the header file "Header.h":
#include "Header.h"
info::info() {
name = "????";
num = 0;
next = NULL;
}
Text.txt file in which I'm reading from as said in the description below.
Ashlee 5
Bill 6
Colby 7
Blake 9
this here is my main.cpp in which I keep getting my read access violation. What Im trying to do is read from the file
Text.txt
putting it into a struct then usingheadInsertion
to make the linked list.
#include<iostream>
#include<fstream>
#include<string>
#include "Header.h"
using namespace std;
void read(ptr& h);
void headInsertion(ptr& h, ptr& Struct);
void printer(ptr& h);
int main() {
ptr h;
read(h);
printer(h);
}
void read(ptr& h) {
ifstream fin;
fin.open("Text.txt");
if (fin.fail()) {
cout << "file not working";
exit(1);
}
string person;
string junk;
getline(fin, person, '\t');
while (!fin.fail()) {
ptr s1 = new info;
s1->name = person;
fin >> s1->num;
headInsertion(h, s1);
s1 = NULL;
getline(fin, junk, '\n');
getline(fin, person, '\t');
}
fin.close();
}
void headInsertion(ptr& h, ptr& Struct) {
Struct->next = h;
h = Struct;
}
void printer(ptr& h) {
for (ptr tr = h; tr!= NULL; tr = tr->next)
{
cout << tr->name<<tr->num; //Exception thrown: read access violation.
//tr was 0xCCCCCCCC.
}
}
And where the comment in the code starts that's where I get it. Im confused since I defined
ptr
in my header file and I still get the output from the linked list onto the console usingptr tr