I am trying to read information in from a file, each line of which contains a name in the form 'last, first middle'. I can successfully open the file and read it into an array that is roughly 2500 string in size. The problem comes when I try to sort the array into alphabetical order. I am using a sorting algorithm that we were instructed to use, found in our book. It is shown in the code. I would add the file that needs to be opened, but it is a few thousand lines long.
The error I am getting is:
Unhandled exception at 0x00A28628 in PeopleSearch.exe: 0xC0000005: Access violation reading location 0x00CC400C.
and I know it has to do with the sorting algorithm from using break points and running the program with that section commented out. I am stumped to why it is giving this error, as it seems that it is trying to access the array outside of the bounds of the array, but it shouldn't be
using namespace std;
int main()
{
string NameArray[2500], filename; //declare array and file to open
ifstream Namesfile;
cout << "This program will take a list of names, sort them, and allow you to either look at the sorted list or search for a specific person.\n";
cout << "Please input the filename:";
//getline(cin, filename);
//Namesfile.open(filename.c_str());
Namesfile.open("Namefile.txt");
if (Namesfile.fail())
{
cout << "\nAn error has occured when trying to open the file. Please verify the file name is correct.\nInput any key to exit:";
int exit;
cin >> exit;
cout << "\nGoodbye\n";
_sleep(2000);
return 0;
}
//reading values from file into the program
int counter = -1;
string Placeholder;
while (getline(Namesfile, Placeholder))
{
if (counter == -1)
{
string dump;
dump = Placeholder;
}
else
{
NameArray[counter] = Placeholder;
}
counter++;
}
//sort names
string minvalue;
int scannumber, minIndex;
for(scannumber = 0; scannumber < (counter -1); scannumber++)
{
minIndex = scannumber;
minvalue = NameArray[scannumber];
for (int index = scannumber+1 ; scannumber < (counter -1); index++)
{
if (NameArray[index] < minvalue)
{
minvalue = NameArray[index];
minIndex = index;
}
}
NameArray[minIndex] = NameArray[scannumber];
NameArray[scannumber] = minvalue;
}
// User interface for selection
cout << "\nWhat would you like to do?\nA. Print the list of people\nB. Search for a name\nX. Exit the program\n";
char choice;
cin >> choice;
if (choice != 'A' && choice != 'B' && choice != 'X' && choice != 'a' && choice != 'b' && choice != 'x')
{
cout << "\nPlease input a valid choice: ";
cin >> choice;
}
while (int x = 1)
{
switch (choice)
{
case 'A':
case 'a':
cout << "You have selected the people search function. Please input a name in the form: """"Last, First Middle""""\n";
break;
case 'B':
case 'b':
cout << "You have selected the list print function. The names are shown sorted below:";
break;
case 'X':
case 'x':
// ending sequence
Namesfile.close();
cout << "\nInput any key to exit:\n";
int exit;
cin >> exit;
cout << "\nGoodbye";
_sleep(800);
return 0;
}
cout << "\nWhat would you like to do?\nA. Print the list of people\nB. Search for a name\nX. Exit the program\n";
cin >> choice;
if (choice != 'A' && choice != 'B' && choice != 'X' && choice != 'a' && choice != 'b' && choice != 'x')
{
cout << "\nPlease input a valid choice: ";
cin >> choice;
}
}
//search for a specific person (run as seperate function)
//void peoplesearch(string NameArray[])
//display the list of names (run as seperate function)
//void listfunction(string NameArray[])
Namesfile.close();
cout << "\nInput any key to exit:\n";
int exit;
cin >> exit;
cout << "\nGoodbye";
_sleep(2000);
return 0;
}
//code
I copied your code into VC6 and ran it. It compiles fine. The second/inner For-Loop just runs forever though. Index keeps incrementing, as you say, outside the bounds of the array.
----------------- Four name names file --------------
Pacer, German Stu
Barnsworth, Dan Grey
Smith, Julie May
Propia, Engelbert Stan
-------------------------------------------------------- names are made up.
On the line:
if (NameArray[index] < minvalue)
index value is 56 on a four-name Namefile.txt file. This is where the access violation occurs. i.e. NameArray[56]
msg: Unhandled exception in TestSort.exe xxxxxx Access Violation.
In looking at the code further, I see a couple of problems...
for these lines:
1 2
string dump;
dump = Placeholder;
...nothing happens with string dump- the first line of the file, after it's read in. It is not added to the NameArray array for comparison/sort.
The first outer -for- loop only works once.
The inner -for- loop is malformed. The controlling loop code:
for (int index = scannumber+1 ; scannumber < (counter -1); index++)
may be OK at first glance, but 'scannumber' never gets incremented. Thus the loop runs forever.
I hope that this gets you started ....
To debug, I found that putting in a bunch of cout << type messages helps me to see what's going on clearer. Then later I surround them with #if debug #endif statements to filter them out for the release version.