Why does the following code crash when i run it{linked lists}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <vector>

using namespace std;

vector<float> csv_input;

struct nodeType// same as struct Customer
{
    float info;
    nodeType *link;
    nodeType *taskList;
    nodeType *wordCount;

};

int counter_ptr;/***Public varriable declaration***/

//Generate random number "1 or 0" that then decides which .csv file to read from or use
void choseInput()
{
    float values;

    srand( time( 0 ) );
    int randNumb = (rand() % 2);
    int counter = 0;

    if (randNumb == 1)
    {
        ifstream inFile("input_1.csv");
        while(inFile>>values)
        {
            csv_input.push_back(values);
            counter ++;
        }

    }
    else if (randNumb == 0)
    {
        ifstream inFile("input_2.csv");
        while(inFile>>values)
        {
            csv_input.push_back(values);
            counter ++;
        }
    }

    counter_ptr = counter;
}
int value = 0;
void number(int numb)
{
    numb = 78;
    numb = value;
}

nodeType* buildListForward()
{
    nodeType *first, *newNode, *last;

    first = NULL;

    for (int i = 0; i< counter_ptr; i++)
    {
        newNode = new nodeType;
        newNode->info = csv_input[i];
        newNode->link = NULL;

        if (first == NULL)
        {
            first = newNode;
            last = newNode;
        }

        else
        {
            last->link = newNode;
            last = newNode;
        }
    }
        cout<<first->info;

    return first;
    //End building list forward

}

class customer
{
public:
    customer();
private:
    nodeType taskList;
    nodeType wordCount;
    nodeType missPelledList;

};



int main()
{
buildListForward();
}

I think calling the buildListForward function in main should cout the info section of the head, but what happends is codeblock crashe, PLEASE HELP! :(
replace your code with this and it should show you where it's having trouble.

1
2
3
4
5
6
7
8
9
10
	cout << "Test1" << endl;
    for (int i = 0; i < counter_ptr; i++)
    {
	cout << "Test1.1" << endl;
        newNode = new nodeType;
	cout << "Test1.2" << endl;
        newNode->info = csv_input[i];
	cout << "Test1.3" << endl;
        newNode->link = NULL;
cout << "Test2" << endl;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <vector>

using namespace std;

vector<float> csv_input;

struct nodeType// same as struct Customer
{
    float info;
    nodeType *link;
    nodeType *taskList;
    nodeType *wordCount;

};

int counter_ptr;/***Public varriable declaration***/

//Generate random number "1 or 0" that then decides which .csv file to read from or use
void choseInput()
{
    float values;

    srand( time( 0 ) );
    int randNumb = (rand() % 2);
    int counter = 0;

    if (randNumb == 1)
    {
        ifstream inFile("input_1.csv");
        while(inFile>>values)
        {
            csv_input.push_back(values);
            counter ++;
        }

    }
    else if (randNumb == 0)
    {
        ifstream inFile("input_2.csv");
        while(inFile>>values)
        {
            csv_input.push_back(values);
            counter ++;
        }
    }

    counter_ptr = counter;
}
int value = 0;
void number(int numb)
{
    numb = 78;
    numb = value;
}

nodeType* buildListForward()
{
    nodeType *first, *newNode, *last;

    first = NULL;

    cout << "Test1" << endl;
    for (int i = 0; i < counter_ptr; i++)
    {
        cout << "Test1.1" << endl;
        newNode = new nodeType;
        cout << "Test1.2" << endl;
        newNode->info = csv_input[i];
        cout << "Test1.3" << endl;
        newNode->link = NULL;
        cout << "Test2" << endl;

        //End building list forward

    }
    return first;

}

class customer
{
public:
    customer();
private:
    nodeType taskList;
    nodeType wordCount;
    nodeType missPelledList;

};

int main()
{
    buildListForward();
}


Do you mean like this: if so i did that and all it does is print out "Test 1 0.o
Compile it, and then type the debug code. :)
Topic archived. No new replies allowed.