Whats up?
I've been trying to teach my brother C++, however, in a recent "Linked List" application I coded, I've been getting errors (compiles fine, they're memory errors) with pointers.
Here's my code:
Code:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class CEmployee
{
public:
string name;
string position;
string phone;
CEmployee * next;
};
void new_employee ( CEmployee * root, string name, string position, string phone )
{
CEmployee * e;
e = root;
if ( root == NULL )
{
root = new CEmployee;
root->name = name;
root->position = position;
root->phone = phone;
root->next = NULL;
}
else
{
while ( e->next != NULL )
e = e->next;
e->next = new CEmployee;
e = e->next;
e->name = name;
e->position = position;
e->phone = phone;
e->next = NULL;
}
}
int main ( int argc, char * argv[] )
{
CEmployee *root;
CEmployee *parse;
root = NULL;
string buffer[3];
char res = NULL;
do
{
cout << "Employee's Name: ";
getline ( cin, buffer[0] );
cout << "Employee's Position: ";
getline ( cin, buffer[1] );
cout << "Employee's Phone Number: ";
getline ( cin, buffer[2] );
new_employee ( root, buffer[0], buffer[1], buffer[2] );
cout << "Add another employee [Y/n]? " <<endl << endl;
res = toupper ( _getch() );
} while ( res == 'Y' );
system("cls");
cout << "List of employees: " << endl << endl;
parse = root;
while ( parse->next != NULL )
{
cout << "Details for " << parse->name << ":" << endl <<
"\tName: " << parse->name << endl <<
"\tPosition: " << parse->position << endl <<
"\tPhone: " << parse->phone << endl << endl;
parse = parse->next;
}
system("pause");
return 0;
}
What happens is at the line "parse = root," windows tells me that the program has done something wrong and now it must be debugged. What have I done wrong in this program?
Thanks in advance for any help,
Guitarist809