/*****************************
* Made by me *
* linked list *
* practice *
*****************************/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
usingnamespace 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
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.
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
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.
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