Three entries and it stops working...

Hello everyone I just started learning C++.

There is a program who is supposed to creat a list of employees. The idea is that each person has a number, and a number can be distributed only once.

At the beginning, it seems that it works fine, but after the third entry, the program just stops, without any warning. It doesn't ask me to quit either. It just stops...

Could anyone tell me how to correct it ?

Thanks!!

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
  #include<iostream>
using namespace std;

class employee
{
private:
	int number;
	char name[20];
public:
	employee() {number=0; strcpy_s(name," ");}
	employee(int x, char *str) {number=x; strcpy_s(name, str);}
	~employee() {}
	int getnumber() {return number;}
	char *getname() {return name;}
	void setnumber(int x) {number=x;}
	void setname(char *str) {strcpy_s(name, str);}
};

class node
{
private:
	employee *thisemployee;
	node *next;
public:
	
	~node() {}
	node( employee *p): thisemployee(p), next(0) {}
	employee *getemployee() {return thisemployee;}
	node *getnext() {return next;}
	void setnext(node *p2) {next=p2;}
	void setemployee(employee *p3) {thisemployee=p3;}
};

class list
{
private:
	node *pHead;
public:
	list(): pHead(0) {}
	~list() {}
	void insert(employee *p);
	void display();
	employee *search(int x);
};

void list::insert(employee *p)
{
	node *pNew=new node(p);
	node *pCurrent=pHead;
	node *pNext=0;

	if(!pHead)
	{
		pHead=pNew;
		return;
	}

	if(pNew->getemployee()->getnumber()<pHead->getemployee()->getnumber())
	{
		pNew->setnext(pHead);
		pHead=pNew;
		return;
	}

	for(;;)
	{
		if(!pHead->getnext())
		{
			pHead->setnext(pNew);
			return;
		}

		pNext=pHead->getnext();
		if(pNew->getemployee()->getnumber()<pNext->getemployee()->getnumber())
		{
			pNew->setnext(pNext);
			pCurrent->setnext(pNew);
			pNext=pNew;
			return;
		}
		pCurrent=pNext;
	}
}

void list::display()
{
	node *pCurrent=pHead;
	while(pCurrent)
	{
		cout<<"Number: "<<pCurrent->getemployee()->getnumber()<<endl;
		cout<<"Name: "<<pCurrent->getemployee()->getname()<<endl;
		cout<<"\n";
		pCurrent=pCurrent->getnext();
	}
}

employee *list::search(int x)
{
	node *pC=0;
	for(pC=pHead; pC!=NULL; pC=pC->getnext())
	{
		if(pC->getemployee()->getnumber()==x)
			break;
	}
	if(pC==NULL)
		return NULL;
	else
		return pC->getemployee();
}


class catalogue : private list
{

public:
	void insert(employee *p);
	void display() {list::display();}
};

void catalogue::insert(employee *p)
{
	if(list::search(p->getnumber()))
	{
		cout<<"The number "<<p->getnumber()<<" is already in the list. Please change a number."<<endl;
		return;
	}
	else
	{
		list::insert(p);
		return;
	}
}

int main()
{
	int num;
	char str[20];
	employee *ptr=0;
	catalogue c;
	for(;;)
	{
		cout<<"Please insert a number. Enter -1 to quit the program: "<<endl;
		cin>>num;
		cin.sync();
		if(num==-1)
		{
			break;
		}
		cout<<"Enter the name: "<<endl;
		cin.getline(str,20);
		
		ptr=new employee(num, str);
		c.insert(ptr);
		
	}

	c.display();
	return 0;
}
Well, you have 2 breaks in your code, add several cout << "Test" statements and see where it's breaking down.
Topic archived. No new replies allowed.