I’m new to C++ and was working on an assignment for a class. We were given a .txt file, have to read information from it, and store it in a linked list, and then print it out to the user. After hours of trying to manipulate the examples we were given, and another couple hours of trying to write the code from scratch, I’m getting closest with a little of both.
The file is called payroll.txt and has about 30 or so lines in this type of format:
Clark Kent 55000 2500 0.07
Lois Lane 65000 1000 0.06
Tony Stark 70000 1500 0.05
Our professor is really big on commenting on our code, so I hope it helps. This is my code:
#include <cstdlib>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAX_STR 100
/* Structure Definition */
typedef struct employeeType Employ;
struct employeeType {
char first[MAX_STR]; /* first name */
char last[MAX_STR]; /* last name */
int salary; /* salary */
int bonus; /* bonus */
double deduc; /* percent deduction */
Employ *next;
};
/* operations on the data */
Employ *ReadRecord();
void PrintRecord(Employ *);
main()
{{
Employ *head, *tail, *newp, *tmp;
head = tail = newp = tmp = NULL;
FILE *in; /* file description */
/* open a file, check if it's there */
if( (in = fopen( "payroll.txt", "r" )) == NULL )
{
printf( "Error opening file\n" );
exit(1);
}
while( newp = ReadRecord() )
{
/* Add object to the list */
if( head == NULL )
{
/* Beginning of the list */
head = newp;
/* Current record */
tail = newp;
}
else
{
/* Previous record reference to new record */
tail->next = newp;
/* Current record */
tail = newp;
}
}
/* End of the list */
tail->next = NULL;
/* Loop through the list */
for( tmp=head; tmp!=NULL; tmp=tmp->next )
{
PrintRecord( tmp );
}
Now when I compile, I get the errors:
[Linker error] undefined reference to ReadRecord()
[Linker error] undefined reference to PrintRecord(employeeType*)
I’m almost sure that the ReadRecord and PrintRecord commands he gave us in the example are Pseudo Code meant to mess us up, but I have no idea what could go there. I’ve been pouring over multiple textbooks and searching for a simple way to fix linker errors online, and have run out of ideas.
If anyone could help me out/point me in the right direction it would be greatly appreciated. A link to a webpage with more info on linked lists and Linker Errors would be even more awesome.
Thanks,
Adam
The linker is complaining that you have referenced the functions
ReadRecordandPrintRecord, but you haven’t written them yet. You can write these functions at the end of the current file. You can start with this template:With these function templates added to the file, the linker should no longer complain about undefined references (since the functions have now been created). However, the code won’t work correctly since these functions don’t actually do anything. You will need to fill in the body of the functions (based on the details of your assignment).
Edit: I have included a few hints (as code comments) in case you don’t know where to begin. For detailed help on parsing data from a text file or displaying information to the screen, consult your textbook (it should have many examples that would help you in this instance).
Update: A few links: