-1

When I run the following code:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int SIZE = 50;

//PROTOTYPES
int loadArrays(string[], int[]);
double computeAverage (int, int[]);
void printEmployees(string[], int[], int);
int addEmployee(string[], int[], int);
int validateYears();
int removeEmployee (string[], int[], int);


//MAIN
int main()
{
    string names[SIZE];
    int experience[SIZE];
    int noRecords;


    cout << "Initial array load:\n\n"<<endl;
    noRecords=loadArrays(names,experience);
    printEmployees(names,experience,noRecords);

    cout << "An employee added:\n\n"<<endl;
    noRecords=addEmployee(names,experience,noRecords);
    printEmployees(names,experience,noRecords);

    cout << "Attempted to add same employee:\n\n"<<endl;
    noRecords=addEmployee(names,experience,noRecords); 
    printEmployees(names,experience,noRecords);

    cout << "Employee deleted:\n\n"<<endl;
    noRecords=removeEmployee(names,experience,noRecords);
    printEmployees(names,experience,noRecords);

    cout << "Attempted to delete same employee load\n\n"<<endl;
    noRecords=removeEmployee(names,experience,noRecords);       
        printEmployees(names,experience,noRecords);

    cout << "Program ended successfully"<<endl;
    system("type employees.dat");
    system("pause");

}

//Functions
int loadArrays(string empName[], int expYears[])
{
    int length=0;
    int ctr=0;
    ifstream fin("employees.dat");
    if (!fin.is_open())
    {
        cout << "File cannot be opened" << endl;
        system("pause");
        exit(-1);
    }
    for (; length < SIZE; length++)
    {
        getline(fin,empName[length]);
        fin >> expYears[length];
        if (fin.eof())
            break;
        fin.ignore(80, '\n');
    }
    cout << ("Successfully loaded arrays!\n\n");
    return length;
}

double computeAverage (int length, int expYears[])
{
    int totalExp;
    double average;
    for (int ctr=0; ctr < length; ctr++)
    {
        totalExp+=expYears[ctr];
    }
    return average = totalExp/length;
}

void printEmployees(string names[], int experience[], int noRecords)
{
    for (int ctr=0; ctr < noRecords; ctr++)
        cout << "Employee's name: " <<names[ctr] << endl << "Years of experience " << experience[ctr] << endl << endl;
    cout <<"There are " << noRecords << " records." << endl << endl;
}

int addEmployee(string name[], int experience[], int noRecords)
{
    string newEmployee;
    int newExperience;
    int index;
    int ctr;
    if (noRecords == SIZE)
        cout << "There is no room for more records!";
    else
    {
        cout << "Input a new employee's name: ";
        getline(cin,newEmployee);
        newExperience = validateYears();
    }
    for (index=0; index < noRecords; index++)
    {
        if (name[index]>=newEmployee)
            break;
        if (name[index]==newEmployee)
            cout << "That name is already in our files";
        else
        {
            for (ctr=noRecords; ctr > index; ctr--)
            {
                name[ctr] = name[ctr-1];
                experience[ctr] = experience[ctr-1];
            }
            name[index] = newEmployee;
            experience[index] = newExperience;
            noRecords++;
        }
    }
    return noRecords;
}

int validateYears()
{
        int newExperience;
    cout << endl << "Input the employees experience: ";
    cin >> newExperience;
    while (newExperience < 0 || newExperience > 100 || cin.fail())
    {
        cout << "Please enter a valid number between one and one-hundred to represent the number of years of experience: ";
        cin.ignore(80, '\n');
        cin >> newExperience;
    }
    return newExperience;
}

int removeEmployee (string names[], int experience[], int noRecords)
{
    int ctr;
    string employeeToRemove;
    cout << "Enter the name of the employee you wish to remove (In 'J. Doe' format): ";
    getline (cin, employeeToRemove);
    for (ctr = 0; ctr < noRecords; ctr++)
    {
        if (employeeToRemove == names[ctr])
            break;
        else if (ctr == noRecords)
            cout << "That name is not in our records";
    }
    for (ctr; ctr < noRecords; ctr++)
    {
        names[ctr] = names[ctr+1];
        experience[ctr] = experience[ctr++];
    }
    noRecords--;
    return noRecords;
}

After I input the employees name and employees years of experience, it give me

Unhandled exception at 0x5a52ca58 (msvcr100d.dll) in Employee.exe: 0xC0000005: Access violation writing location 0xf18be2b7.

I have tried moving around variable but I don't see the problem. Thank you in advance!

Tim
  • 41,901
  • 18
  • 127
  • 145
Stephen Sparks
  • 113
  • 1
  • 8
  • 2
    Have you tried using a debugger? – Nick May 16 '14 at 13:27
  • @Nick I'm very new to this, so I don't know what you mean. – Stephen Sparks May 16 '14 at 13:30
  • Might be related to [this](http://stackoverflow.com/questions/4241882/unhandled-exception-at-0x523d14cf-msvcr100d-dll) and [this](http://stackoverflow.com/questions/19314789/unhandled-exception-at-msvcr100d-dll). – Shoe May 16 '14 at 13:34
  • 2
    @StephenSparks: A debugger is a program that helps you find errors in a program. With the debugger, you can step through your program line by line and look at how the variables change values, and many other very useful things. As a programmer, you really need to try it! – Thomas Padron-McCarthy May 16 '14 at 13:36
  • I'm taking a college course on this and I program in Microsoft Visual Studio. Not sure if that counts as a debugger. – Stephen Sparks May 16 '14 at 13:37
  • From your error I deduce that you use Visual Studio. Investigate its debugger, it is excellent. It will save you a !lot! of pain in the future if you spend just an hour now to figure out how it can be used! – Dirk May 16 '14 at 13:38
  • In C++ `int totalExp;` does not automatically initialize your variable to 0. You must initialize it explicitly: `int totalExp = 0;`. I don't know if this is related to your problem, but you should stop worrying about your problem, initialize all your variables, and then start worrying about it again. – Daniel Daranas May 16 '14 at 13:41
  • @DanielDaranas I just changed it all so it initializes everything before it is used. Same problem. :S – Stephen Sparks May 16 '14 at 13:44
  • 3
    Stop using pointers (C-arrays) and pick a proper C++ container, unless you know what you are doing! –  May 16 '14 at 13:45
  • @DieterLücking This is an assignment so I have to use what we were taught in class. – Stephen Sparks May 16 '14 at 13:46
  • @StephenSparks And what about that debugger? – Daniel Daranas May 16 '14 at 13:53
  • @DanielDaranas if you are referring to when I click "Debug", nothing is happeneing – Stephen Sparks May 16 '14 at 13:54
  • 3
    @StephenSparks Listen to me carefully. You need to learn how to use the debugger. [Where to start?](https://www.google.es/search?q=Microsoft+Visual+Studio+learning+to+use+the+debuggert&oq=Microsoft+Visual+Studio+learning+to+use+the+debuggert&aqs=chrome..69i57.7268j0j1&sourceid=chrome&ie=UTF-8#q=Microsoft+Visual+Studio+learning+how+to+use+the+debugger&safe=off) – Daniel Daranas May 16 '14 at 13:58

1 Answers1

0
    for (index; index < noRecords; index++)
    {
        if (name[index]>=newEmployee)
            break;
    }
    if (name[index]==newEmployee)
        cout << "That name is already in our files";
    else
    {
        for (ctr=noRecords; ctr > index; ctr--)
        {
            name[ctr] = name[ctr-1];
            experience[ctr] = experience[ctr-1];
        }
        name[index] = newEmployee;
        experience[index] = newExperience;
        noRecords++;
    }
return noRecords;
}

The for loop looking for the insertion point was doing more than just searching for the inserting point. Moved some brace brackets and tadaaa~

Thanks for all your help, I'm going to read some debugging tutorials.

Stephen Sparks
  • 113
  • 1
  • 8