0

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 using headInsertionto 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 using ptr tr

Andy
  • 31
  • 3
  • `typedef info* ptr;` - typedef pointers are bad (style), they obfuscate obvious things, which I guess is the source of your problems.. Just use `info*`. `while (!fin.fail())` [while(feof) is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) `ptr& h` - you are passing a reference to a pointer. The data where the pointer points to is most probably uninitialized. – KamilCuk Apr 13 '20 at 20:34
  • @Andy You did not initialize to nullptr the initial pointer ptr h; – Vlad from Moscow Apr 13 '20 at 20:38
  • And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Apr 13 '20 at 20:39
  • Wow, that was an easy fix. When I was debugging, I didn't check my code in main. Thanks. – Andy Apr 13 '20 at 20:47

1 Answers1

0
int main() {
    ptr h;
    read(h)

You pass an uninitialized ptr to read.

void read(ptr& h) {
// snip of code that does not touch h
    headInsertion(h, s1);

The unititialized ptr is passed to headInsertion.

void headInsertion(ptr& h, ptr& Struct) {
    Struct->next = h;
    h = Struct;
}

Ooops, that first line sets Struct->next to the value of h, but h was never initialized.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278