help getting program to run until stop is entered and memory access errors

hello I'm am needing help with some code I wrote that reads a text file of 1000 most popular female names and sorts them for the user to enter a name and outputs its popularity number. I need to have the program keep asking the user to enter names until stop is entered and I am unsure how to do this. Also I am told my program has memory access errors becuase This got memory access errors because it tried to referenced femnames[1000] which doesn't exist, i dont understand as i have femnames[SIZE] in which SIZE=1000. below is my code, any help would be appreciated.

main.cpp
#include "Header.h"
#include<iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <time.h>
#include<ctime>

using namespace std;

void sortNames(FemaleNames[], int, int);
int linearSearch(FemaleNames *femnames, string searchKey, int count);
int binarySearch(FemaleNames *femnames, string searchKey, int count);
string toUpper(string name);
const int SIZE = 1000;

int main()

{
FemaleNames femnames[SIZE];
string name;
int number;
int count = 0;
string searchKey;
ifstream infile("CommonFemaleNames.txt");

if (infile)
{
infile >> number;
infile >> name;
femnames[count].setNumber(number);
femnames[count].setName(name);

while (!infile.eof())
{
count++;
infile >> number;
infile >> name;
femnames[count].setNumber(number);
femnames[count].setName(name);

}

}

infile.close();
sortNames(femnames, 0, SIZE - 1);

clock_t startClock, finishClock;

double timeCount;

cout << "Enter a female name : ";
cin >> searchKey;

searchKey = toUpper(searchKey);

startClock = clock() / 100000;

int pos1 = linearSearch(femnames, searchKey, count);

finishClock = clock() / 100000;

timeCount = finishClock - startClock;

cout << "Linear search took " << timeCount << " microseconds." << endl;

startClock = clock() / 100000;

int pos2 = binarySearch(femnames, searchKey, count);

finishClock = clock() / 100000;

timeCount = finishClock - startClock;

cout << "Binary search took " << timeCount << " microseconds." << endl;

if (pos1 != -1)

cout << searchKey << " is the number " << pos1 + 1 << " most popular female name" << endl;

else

cout << "Name not found" << endl;

system("pause");

return 0;

}

string toUpper(string name)

{
for (int i = 0; i<name.size(); i++)

{
name[i] = toupper(name[i]);
}

return name;

}

void sortNames(FemaleNames femnames[], int left, int right)

{

int i = left, j = right;

FemaleNames temp;

string pivot = femnames[(left + right) / 2].getName();

while (i <= j) {

while (femnames[i].getName()<pivot)

i++;

while (femnames[j].getName()>pivot)

j--;

if (i <= j) {

temp = femnames[i];

femnames[i] = femnames[j];

femnames[j] = temp;

i++;

j--;

}

};

if (left<j)

sortNames(femnames, left, j);

if (i<right)

sortNames(femnames, i, right);

}

int linearSearch(FemaleNames *femnames, string searchKey, int count) {

for (int i = 0; i <= count; ++i)
{

if (femnames[i].getName().compare(searchKey) == 0)

return i;

}

return -1;

}

int binarySearch(FemaleNames *femnames, string searchKey, int count)

{

int first = 0, last = count + 1, middle;
bool found = false;

while (!found && first < last)

{
middle = (first + last) / 2;

if ((femnames[middle].getName().compare(searchKey)) == 0)
{

return middle;

}

else if ((femnames[middle].getName().compare(searchKey)) > 0)

last = middle - 1;

else

first = middle + 1;

}

return -1;

}

source.cpp
#include <stdio.h>
#include "Header.h"


FemaleNames::FemaleNames()
{
this->number = 0;
this->name = "";
}

int FemaleNames::getNumber()
{
return this->number;
}

string FemaleNames::getName()
{
return this->name;
}

void FemaleNames::setNumber(int number)
{
this->number = number;
}

void FemaleNames::setName(string name)
{
this->name = name;
}

header.h
#ifndef Header_H
#define Header_H

#include<iostream>
#include<string>

using namespace std;

class FemaleNames
{
private:
int number;
string name;

public:
FemaleNames();
int getNumber();
string getName();
void setNumber(int number);
void setName(string name);

};

#endif




Last edited on
> keep asking the user to enter names until stop is entered
while(cin >> searchKey and searchKey not_eq "stop")

> it tried to referenced femnames[1000] which doesn't exist, i dont understand as i have femnames[SIZE] in which SIZE=1000.
first element is femnames[0]
last element is femnames[999]
valid index goes from 0 to size-1
Thank you so much for your help on the while statement but Im not sure what you mean by your response to the femnames issue. Im sorry im still new to all this and have a hard time understanding.
Last edited on
The first element in the array is the one at offset zero from the beginning of the array.

For example, in the array of 5 elements int a[5]
The first element exists exactly at the beginning of the array - that is, zero elements offset from the beginning of the array, and is so named a[0].
The fifth element exists at an offset of four elements from the beginning of the array; that is, it has four elements before it, and therefore the last element in the array is named a[4].

It is forbidden to access a[5], because that would access at the sixth element in a five element array.

See:
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Topic archived. No new replies allowed.