Need help finding an error

Doing a project for a class and neither I nor the teacher can find the problem upon first look. Am I missing something here?

The project includes writing a program that sorts information from a text document into arrays, sorting them by age, and then letting the user search by name, and conduct multiple searches. Initially it worked fine, but when I showed the instructor, all searches come up with "No results found."

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

#include<iostream>
#include<fstream>
#include<string>
using namespace std;


void main()
{
	
	ifstream fin;
	fin.open("C:\\Users\\001397744\\Documents\\list.txt");

	
	int i, id[20], age[20], maxRecords, a, temp, guess, low, high;
	char ans;
	string searchName, surename[20], tempName, birthMonth[20], tempMonth;

	i = 0;
	a = 0;
	ans = 'y';

	while(!fin.eof())
	{
		fin >> id[i] >> surename[i] >> age[i] >> birthMonth[i];
		i++;
	}
	maxRecords = i;

	fin.close();

	for(i = 0; i < maxRecords - 1; i++)
	{
		for(a = 0; a < maxRecords - 1; a++)
		{
			if(age[a] > age[a + 1])
			{
				temp = age[a];
				age[a] = age[a + 1];
				age[a + 1] = temp;
	
				temp = id[a];
				id[a] = id[a + 1];
				id[a + 1] = temp;
	
				tempName = surename[a];
				surename[a] = surename[a + 1];
				surename[a + 1] = tempName;
	
				tempMonth = birthMonth[a];
				birthMonth[a] = birthMonth[a + 1];
				birthMonth[a + 1] = tempMonth;
			}
		}
	}

	cout << "Sorted list by age" << endl << endl;

	for(i = 0; i < maxRecords - 1; i++)
	{
		cout << id[i] << " " << surename[i] << " " << age[i] << " " << birthMonth[i] << endl;
	}

	cout << endl << endl;

	while(ans == 'y')
	{
		cout << endl;
		cout << "Please enter a last name to search: ";
		cin >> searchName;

		low = 0;
		high = 19;

		guess = (low + high) / 2;

		while(low < high && searchName != surename[guess])
		{
			if(searchName < surename[guess])
			{
				high = guess - 1;
			}
			else if(searchName > surename[guess])
			{
				low = guess + 1;
			}

			guess = (low + high) / 2;

		}

		if(searchName == surename[guess])
		{
			cout << endl;
			cout << "ID: " << id[guess] << ". Name: " << surename[guess] << ". Age: " << age[guess] << ". Month: " << birthMonth[guess] << endl;
		}
		else
		{
			cout << endl;
			cout << "That name was not found in the array." << endl;
		}

		cout << "New search? (y for yes)" << endl << endl;
		cin >> ans;

	}

	cout << endl;
	system("pause");

}
The first thing I see wrong is void main(), in C++ main() must be defined to return an int, and you should return an int from this function.

Next:
1
2
3
4
5
	while(!fin.eof())
	{
		fin >> id[i] >> surename[i] >> age[i] >> birthMonth[i];
		i++;
	}

Using eof() to control your read loop is wrong. You should use the read operation to control the loop. You also need to insure that you don't exceed the size of your array. Something like:
1
2
3
4
5
	while(i < 20 && fin >> id[i] >> surename[i] >> age[i] >> birthMonth[i];)
	{
		
		i++;
	}

You should also be using a named constant for your array sizes instead of the magic number (20).

Next:
for(i = 0; i < maxRecords - 1; i++)
Why the - 1? The need for this is probably being caused by the use of eof() to control your read loop. You increment i once too many times because of the eof().

And don't forget your strings can't have any whitespace characters embedded within them because you're using the extraction operator>>.

Lastly are you sure the file is opening correctly? You really should check that it opens correctly because your professor probably doesn't have a directory named "C:\Users\001397744\Documents\"
Last edited on
@jlb Thanks for the quick reply! A few things:

1. I don't need to define the main() function because I'm not trying to return an integer, I am simply trying to conduct a binary search in the array, then the user can exit the code once done. (At least, I'm fairly sure but obviously a beginner)

2. For the purposes of this assignment, I'm trying to stick to methods that the instructor has taught so far.

3. There are no white-spaces within the strings in the file itself. The file is created by me, and they are all regular names with regular characters.

4. That directory is on my computer. The user is my student ID, and I am not sending the file to be opened by the instructor; she is simply looking over my shoulder at my monitor and checking if it runs correctly (which, update: it works for only certain names when I run the program.)
1. I don't need to define the main() function because I'm not trying to return an integer,

It doesn't matter what you're trying to do, in a C++ program (for a hosted system) main() must be defined to return an int.
Look what the embedded compiler (gear at the top right of your code) says:
8:11: error: '::main' must return 'int'


For the purposes of this assignment, I'm trying to stick to methods that the instructor has taught so far.

Such as?

Using eof() to control a loop will usually end up, as is the case in your program, of having one extra read. The eof() flag is not set until after you try to read past the end of file, which means that you're incrementing the counter variable one too many times. Also not checking that you're not overflowing the bounds of your arrays is a buffer over flow error waiting to happen.

she is simply looking over my shoulder at my monitor and checking if it runs correctly (which, update: it works for only certain names when I run the program.)

Well since I'm not looking over your shoulder I can't address this problem until you clarify what you 're talking about. It would be helpful if you posted a sample of your input file and examples of what you're inputting into your program. And don't forget that C++ is case sensitive, "Sample" is not the same as "sample".

And you really really need to check that the file opens correctly, a file failing to open is a very common failure point.

Last edited on
Topic archived. No new replies allowed.