Employee database: debug assertion failed

When I run the program, i get debug assertion failed. What part of the program is wrong?

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

int findData(int option, struct customerRecords employee[20], char search[4]);
void displayData(int i);

struct customerRecords
	{
		char name[20];
		int uid;
	}employee[20];

int main( )
{
	//employee database
	strcpy(employee[0].name, "House");
	employee[0].uid=1234;
	strcpy(employee[1].name, "Wilson");
	employee[1].uid=7387;
	strcpy(employee[2].name, "Cuddy");
	employee[2].uid=2907;
	strcpy(employee[3].name, "Chase");
	employee[3].uid=8632;
	strcpy(employee[4].name, "Foreman");
	employee[4].uid=4526;
	strcpy(employee[5].name, "Cameron");
	employee[5].uid=7327;
	strcpy(employee[6].name, "Taub");
	employee[6].uid=2580;
	strcpy(employee[7].name, "Hadley");
	employee[7].uid=7610;
	
	//input
	char search[20];
	int number;
	cout << "Search for name or uid: ";
	cin >> search;
	for (int i = 0; i < 20; i++)
	{
		if (isalpha(search[i]))
			number = findData(1, employee, search);
		else if (isdigit(search[i]))
			number = findData(2, employee, search);
		else if (search[0] == '-' && search[1] == '1')
		{
			system ("pause");
			exit(1);
		}
		else
			cout << search << "\nYou must enter a valid name or uid.";
	}

	displayData(number);

	system ("pause");
	return 0;
}

int findData(int option, struct customerRecords employee[20], char search[20])
{
	int output = 0;
	for (int i=0; i<20; i++)
	{
		switch (option)
		{
		case 1:
			if (search == employee[i].name)
			{
				output = i;
				i = 20;
			}
			break;
		case 2:
			int uid = atoi (search);
			if (uid == employee[i].uid)
			{
				output = i;
				i=20;
			}
			else
				cout << "No match.";
			break;

		}
	}
	return (output);
}

void displayData(int i)
{
	cout << employee[i].name << " " << employee[i].uid;
}
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
int findData(int option, struct customerRecords employee[20], char search[20])
{
	int output = 0;
	for (int i=0; i<20; i++)
	{
		switch (option)
		{
		case 1:
			if (search == employee[i].name)
			{
				output = i;
				i = 20;
			}
			break;
		case 2:
			int uid = atoi (search);
			if (uid == employee[i].uid)
			{
				output = i;
				i=20;
			}
			else
				cout << "No match.";
			break;

		}
	}
	return (output);
}


You have a global variable called "employee". The above function has a parameter called "employee". This function's parameter hides the global variable employee. So basically, it's an unitialized array in this function. You should change the name of this parameter in this function.

Also, you don't need to put struct in front of the parameter type.

Above main, I would change the code to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
using namespace std;

struct customerRecords
{
     char name[20];
     int uid;
}employee[20];

int findData(int option, customerRecords employeeArray[20], char search[4]);
void displayData(int i);

int main( )
...


since one of your prototypes depends on the struct.
I fixed it, but it still says debug assertion failed. What causes this?
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
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
using namespace std;

struct customerRecords
	{
		char name[20];
		int uid;
	}employee[8];
int findData(int option, struct customerRecords emp[8], char search[20]);
void displayData(int i);

int main( )
{
	strcpy_s(employee[0].name, "House");
	employee[0].uid=1234;
	strcpy_s(employee[1].name, "Wilson");
	employee[1].uid=7387;
	strcpy_s(employee[2].name, "Cuddy");
	employee[2].uid=2907;
	strcpy_s(employee[3].name, "Chase");
	employee[3].uid=8632;
	strcpy_s(employee[4].name, "Foreman");
	employee[4].uid=4526;
	strcpy_s(employee[5].name, "Cameron");
	employee[5].uid=7327;
	strcpy_s(employee[6].name, "Taub");
	employee[6].uid=2580;
	strcpy_s(employee[7].name, "Hadley");
	employee[7].uid=7610;
	
	char search[20];
	int number = 0, length = strlen(search);
	cout << "Search for name or uid: ";
	cin >> search;
	for (int i = 0; i < length; i++)
	{
		if (isalpha(search[i]))
			number = findData(1, employee, search);
		else if (isdigit(search[i]))
			number = findData(2, employee, search);
		else if (search[0] == '-' && search[1] == '1')
		{
			system ("pause");
			exit(1);
		}
		else
			cout << search << "\nYou must enter a valid name or uid.";
	}

	displayData(number);

	system ("pause");
	return 0;
}

int findData(int option, struct customerRecords emp[8], char search[20])
{
	int output = 0, length = strlen(search);
	switch (option)
	{
	case 1:
		for (int i = 0; i<length; i++)
		{
			if (search == employee[i].name)
			{
				output = i;
			}
		}
		break;
	case 2:
		for (int i = 0; i<4; i++)
		{
			int uid = atoi (search);
			if (uid == employee[i].uid)
			{
				output = i;
			}
		}
		break;
	}
	return (output);
}

void displayData(int i)
{
	cout << employee[i].name << " " << employee[i].uid;
}
1
2
char search[20];
int number = 0, length = strlen(search);

Here, you're calling strlen on an uninitialized char array. You should do this only after the the call to cin>>. Also, if your requirements permit, I would suggest using std::strings as opposed to char arrays. That way you wouldn't have to limit the amount of characters entered.

if (search == employee[i].name)

You also can't compare char arrays in this manner. This statement would always return false since it's comparing the address of the first char in each array (they're different in this case). You need to use strcmp or strncmp to compare char arrays.
Last edited on
Topic archived. No new replies allowed.