I am trying to recursively sort by job title. Job Title is declared in the Employee class. Whenever I attempt to recursively sort the stack, the popping is done for each stack data member, but the compiler throws this error soon after. I am curious about what this error means as I have done research but it does not seem to be a definite cause.
Note: The program begins in main by getting the employee data and has a few options as to what can be done with the data. In this entering 5 in the main function would sort the data.
main.cpp
#include <iostream>
#include <iomanip>
#include "employees.h"
usingnamespace std;
void show_menu();
int find_search_pos();
int main() {
Employee record[10],temp;
int num_of_employees;
Stack database;
int pos = 0,choice =-1;
while (choice !=7) {
show_menu();
cin >> choice;
switch (choice) {
case 1:
cout<<"\nHow many employees would you like to enter data for: ";
cin>>num_of_employees;
for(int i = 0; i< num_of_employees; i++) {
cout<<"\nEnter data for employee number "<<pos+1<<".\n";
temp.storedata();
record[pos] = temp;
database.Push(record[pos]);//store 1 of 10 employees
pos++;
}
break;
case 3:
cout<<"\nEnter data for employee number "<<pos+1<<".\n";
temp.storedata();
record[pos] = temp;
database.Push(record[pos]);//store 1 of 10 employees
pos++;
break;
case 4:
cout<<"\nRemoving employee...";
database.return_top();
database.Pop();
break;
case 5:
cout<<"\nSorting List: ";
database.Sort(database);
case 6:cout<<"\n\ndisplay data: ";
database.disp();
break;
}
}
0xC0000005 is windows telling you that your program tried to access memory that it doesn't own.
Typically, this is either a NULL pointer or a pointer to some memory you used to have.
What you need to do now is learn all about the debugger that comes with your development environment.
$ g++ -g foo.cpp
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) run
Starting program: ./a.out
1
How many employees would you like to enter data for: 2
Enter data for employee number 1.
Please enter first name: Fred
Please enter last name: Flintstone
Please enter job title: Breaker
Enter data for employee number 2.
Please enter first name: Barney
Please enter last name: Rubble
Please enter job title: Mover
5
Sorting List:
New data assigned to j:
The name being popped:
First name: Barney
Last name: Rubble
Job Title: Mover
New data assigned to j:
The name being popped:
First name: Fred
Last name: Flintstone
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400d92 in Stack::Sort (this=0x7fffffffda50, s=...) at foo.cpp:90
90 j = temp->worker;
(gdb) bt
#0 0x0000000000400d92 in Stack::Sort (this=0x7fffffffda50, s=...) at foo.cpp:90
#1 0x0000000000400e3b in Stack::Sort (this=0x7fffffffda50, s=...) at foo.cpp:95
#2 0x0000000000400e3b in Stack::Sort (this=0x7fffffffda50, s=...) at foo.cpp:95
#3 0x0000000000401321 in main () at foo.cpp:146
(gdb) p temp
$1 = (Node *) 0x0
(gdb) list
85 }
86
87 void Stack::Sort (Stack & s){
88 Employee j;
89 Node *temp= Top;
90 j = temp->worker;
91 std::cout<<"\nNew data assigned to j: ";
92 j.job_title;
93 if (Top!=NULL){
94 s.Pop();
In short, temp is a NULL pointer and you didn't check it.
A GUI based debugger works in more or less the same way, and will give you the same information.
But for example, when it stops, it will be pointing you directly to the line of code causing the problem.
Thank you for that! I actually am researching how to use the debugger tool in my ide.
any suggestions on how I could get rid of this issue? I did some research on the topic and the error the compiler was throwing.