Odd errors - linked list headache

Hey, It's me again. I'm having some trouble with this program. I'm not sure why I'm getting errors. It all looks correct to me.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*****************************
 *  Made by me               *
 *  linked list              *
 *  practice                 *
 *****************************/

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

struct Node{
	char parts[100];
	long ID;
	int date;
	float score;
	Node *next;
};

void READIN(Node *,string);
void PRINTLIST(Node *);
void AVGSCORE(Node *);
void MAXSCORE(Node *);
void MINSCORE(Node *);
Node REVLIST(Node *);

int main()
{
	string input_file = "a7.txt";
	Node *head_node = NULL;

	READIN(head_node, input_file);
	AVGSCORE(head_node);
	MAXSCORE(head_node);
	MINSCORE(head_node);
	Node *rev_node = REVLIST(Node head_node);

	return 0;
}

void READIN(Node *head_node, string in_file)
{
	ifstream in_data;
	char temp[100];
	Node *temp_node2;

	in_data.open(in_file.c_str());
	while (!in_data.eof())
	{
		Node *temp_node = new Node();

		in.getline(temp, 100);
		temp_node->parts = temp;

		in.getline(temp, 100);
		temp_node->ID = atol(temp);	//converts string to long

		in.getline(temp, 100);
		temp_node->date = atoi(temp);	//converts string to int

		in.getline(temp, 100);
		temp_node->score = atoi(temp);	//converts string to int

		in.getline(temp, 100);
		temp_node->next = NULL;

		if (head_node == NULL)
			head_node = temp_node;
		else
		{
			temp_node2 = head_node;	//starts at the beginning

			while (temp_node2->next != NULL)	// goes through the list one at a time until it finds the end
				temp_node2 = temp_node2->next;

			temp_node2->next = temp_node;	//links last node to temp_node at the end
		}

	}
	in_data.close();

}

void PRINTLIST(Node *head_node)
{
	Node *list = head_node;

	while(list != NULL)
	{
		cout << list->parts << '\n';
		cout << list->ID << '\n';
		cout << list->date << '\n';
		cout << list->score << '\n' << '\n';
		list = list->next;
	}
}

void AVGSCORE(Node *head_node);
{
	Node avgNode = head_node;
	float avgScore;
	int count;

	while(avgNode->next != NULL)
	{
		avgScore += avgNode->score;
		avgNode = avgNode->next;
		count++;
	}

	cout << "The average score is " << avgNode/count << "./n";

}

void MAXSCORE(Node *head_node)
{
	Node maxNode = head_node;
	float maxScore = -999999;

	while(maxNode->next != NULL)
	{
		if (maxNode->score > maxScore)
			maxScore = maxNode->score;

		maxNode = maxNode->next;
	}

	cout << "The max score is " << maxScore << ".\n";

}

void MAXSCORE(Node *head_node)
{
	Node minNode = head_node;
	float minScore = 999999;

	while(minNode->next != NULL)
	{
		if (minNode->score < minScore)
			minScore = minNode->score;

		minNode = minNode->next;
	}

	cout << "The min score is " << minScore << ".\n";

}

Node REVLIST(Node *orig_node)
{
	Node *nh = NULL;
	Node *l = orig_node;

	while(l != NULL)
	{
		if (nh == NULL)
		{
			Node *temp = new Node();
			temp->next = NULL;

			temp->parts = l->parts;	//copying info
			temp->ID = l->ID
			temp->date = l->date;
			temp->score = l->score;

			nh = temp;
		}
		else
		{
			Node *temp = new Node();
			temp->next = NULL;

			temp->parts = l->parts;	//copying info
			temp->ID = l->ID
			temp->date = l->date;
			temp->score = l->score;

			temp->next = nh;
			nh = temp;
		}

		l = l->next;
	}

	return nh;

}
me@me:~/code$ g++ linked.cpp -o linked.out
linked.cpp: In function ‘int main()’:
linked.cpp:37: error: expected primary-expression before ‘head_node’
linked.cpp: In function ‘void READIN(Node*, std::string)’:
linked.cpp:53: error: ‘in’ was not declared in this scope
linked.cpp:54: error: invalid array assignment
linked.cpp: At global scope:
linked.cpp:100: error: expected unqualified-id before ‘{’ token
Last edited on
I'll help you a little since you at least took the time to use tags properly. As you can see the function returns a Node object but in your main you are trying to assign an object to a pointer.

1
2
Node REVLIST(Node *);
Node *rev_node = REVLIST(Node head_node);


What is the name of the file stream object?

1
2
3
4
5
6
7
8
9
10
11
12
13
void READIN(Node *head_node, string in_file)
{
	ifstream in_data;
	char temp[100];
	Node *temp_node2;

	in_data.open(in_file.c_str());
	while (!in_data.eof())
	{
		Node *temp_node = new Node();

		in.getline(temp, 100);
		temp_node->parts = temp;


I'm not going through the rest of the laundry list. most of the errors are pretty obvious.
Last edited on
Instead of giving yourself headaches a better exercise would be to learn how to use the std containers.
http://cplusplus.com/reference/stl/list/
http://cplusplus.com/forum/articles/10879/
Thanks. I went through my code and fixed tons of errors. Now, I only have one error left. I couldn't find anything useful on google.

me@me:~/code$ g++ linked.cpp -o linked.out
linked.cpp: In function ‘Node* READIN(Node*, std::string)’:
linked.cpp:54: error: ‘in’ was not declared in this scope
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*****************************
 *  Made by me               *
 *  linked list              *
 *  practice                 *
 *****************************/

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

struct Node
{
	char parts[100];
	long ID;
	int date;
	float score;
	Node *next;
};

Node *READIN(Node *,string);
void PRINTLIST(Node *);
void AVGSCORE(Node *);
void MAXSCORE(Node *);
void MINSCORE(Node *);
Node REVLIST(Node *);

int main()
{
	string input_file = "a7.txt";
	Node *head_node = NULL;

	head_node = READIN(*&head_node, input_file);
	AVGSCORE(head_node);
	MAXSCORE(head_node);
	MINSCORE(head_node);
	Node rev_node = REVLIST(*&head_node);

	return 0;
}

Node *READIN(Node *head_node, string in_file)
{
	ifstream in_data;
	char temp[100];
	Node *temp_node2;

	in_data.open(in_file.c_str());
	while (!in_data.eof())
	{
		Node *temp_node = new Node();

		in.getline(temp, 100);
		temp_node->parts[100] = temp[100];

		in.getline(temp, 100);
		temp_node->ID = atol(temp);	//converts string to long

		in.getline(temp, 100);

		temp_node->date = atoi(temp);	//converts string to int

		in.getline(temp, 100);
		temp_node->score = atoi(temp);	//converts string to int

		in.getline(temp, 100);
		temp_node->next = NULL;

		if (head_node == NULL)
			head_node = temp_node;
		else
		{
			temp_node2 = head_node;	//starts at the beginning

			while (temp_node2->next != NULL)	// goes through the list one at a time until it finds the end
				temp_node2 = temp_node2->next;

			temp_node2->next = temp_node;	//links last node to temp_node at the end
		}

	}
	in_data.close();

	return head_node;

}

void PRINTLIST(Node *head_node)
{
	Node *list = head_node;

	while(list != NULL)
	{
		cout << list->parts << '\n';
		cout << list->ID << '\n';
		cout << list->date << '\n';
		cout << list->score << '\n' << '\n';
		list = list->next;
	}
}

void AVGSCORE(Node *head_node)
{
	Node *avgNode = head_node;
	float avgScore;
	int count;

	while(avgNode->next != NULL)
	{
		avgScore += avgNode->score;
		avgNode = avgNode->next;
		count++;
	}

	cout << "The average score is " << avgScore/count << "./n";

}

void MAXSCORE(Node *head_node)
{
	Node *maxNode = head_node;
	float maxScore = -999999;

	while(maxNode->next != NULL)
	{
		if (maxNode->score > maxScore)
			maxScore = maxNode->score;

		maxNode = maxNode->next;
	}

	cout << "The max score is " << maxScore << ".\n";

}

void MINSCORE(Node *head_node)
{
	Node *minNode = head_node;
	float minScore = 999999;

	while(minNode->next != NULL)
	{
		if (minNode->score < minScore)
			minScore = minNode->score;

		minNode = minNode->next;
	}

	cout << "The min score is " << minScore << ".\n";

}

Node REVLIST(Node *orig_node)
{
	Node *nh = NULL;
	Node *l = orig_node;

	while(l != NULL)
	{
		if (nh == NULL)
		{
			Node *temp = new Node();
			temp->next = NULL;
			//copying info
			temp->parts[100] = l->parts[100];
			temp->ID = l->ID;
			temp->date = l->date;
			temp->score = l->score;

			nh = temp;
		}
		else
		{
			Node *temp = new Node();
			temp->next = NULL;
			//copying info
			temp->parts[100] = l->parts[100];
			temp->ID = l->ID;
			temp->date = l->date;
			temp->score = l->score;

			temp->next = nh;
			nh = temp;
		}

		l = l->next;
	}

	return *nh;

}
closed account (jwC5fSEw)
The error is totally self-explanatory. Look in the scope of READIN: where are you declaring "in"?

Also, just for future reference, anything in capital letters usually represents a preprocessor macro, not a function. People reading your code could be confused, so it's better to just use lower-case letters in function names.
You called your ifstream "in_data", but you are trying to use it as if it is called "in".
Thanks, I was looking at an example while doing this. I thought in.getline was a function of getline. Also, I'll remember not to use caps for my function names. Now, I need to figure out why it's maxing out my cpu lol
Topic archived. No new replies allowed.