Program crashing?

So I am new to c++ (been doing it for about 4 months now).. anyway, for a class assignment I had to create a program(more or less) that allowed the user to append or show records or exit the program.

It works great, however whenever i hit show records it prints out all the records then crashes. I cant figure it out. Anyway my code is below, I hope I can figure out whats causing this ><..

P.S: When i try to debug it I get this error:
Unhandled exception at 0x0f6d1f68 (msvcp100d.dll) in LAB07A_Gleckman_C.exe: 0xC0000005: Access violation reading location 0xabababab.

I tried looking up that error and got no where.


Code:


/*
Programming Assignment: LAB07A
Developer: cgleckman
Date Written: 06-15-2012
Purpose: Address Database
*/


//Specification: Append and display records in a address database
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "TestAddress.txt";

int main ()
{
menu();
return 0;
} //end main

void menu(void)
{
char menuInput = ' ';

//allow user to choose to append records, display records or exit the program
do // start do while loop
{
// print out the menu and accept input
cout << "(A)ppend Records, (S)how Records, (E)xit" << endl;
cin >> menuInput;
cin.ignore();

switch(toupper(menuInput)) {
case 'A':
writeData();
break;
case 'S':
readData();
break;
case 'E':
cout << "Good-Bye" << endl;
break;
default:
cout << "Invalid Character Entered: Please try again" << endl;
break;
}// end switch
}while (menuInput != 'E'); // end do-while
}//end menu

void writeData(void)
{
// declare local variables
char menuInput = 'Y';
string name = " ";
string street = " ";
string city = " ";
string state = " ";
string zipCode = " ";

// create a ofstream object outInfo and opens it for appending
ofstream outInfo(FileName, ios::app);

//start do while loop
do
{
// get all of the information
cout << "Name: " << endl;
getline(cin, name);
cout << "Street: " << endl;
getline(cin, street);
cout << "City: " << endl;
getline(cin, city);
cout << "State: " << endl;
getline(cin, state);
cout << "Zip Code: " << endl;
getline(cin, zipCode);

//Write the Info to a file
outInfo << name << "," << street << "," << city << "," << state << "," << zipCode << endl;

// check to see if user wants to do another record
cout << "Enter Another Record? (Y/N) " << endl;
cin >> menuInput;
cin.ignore();

}while (menuInput == 'Y' && menuInput == 'y');

outInfo.close();
}//end write data

void readData(void)
{
//open the file to be read in
ifstream inInfo(FileName, ios::in);

string lineBuffer;

while (!inInfo.eof())
{
getline(inInfo, lineBuffer);
//use the split function to break a
//deliminated line of text into fields
string *theFields = split(lineBuffer, ',');

cout << "Name... " << theFields[0] << endl;
cout << "Street... " << theFields[1] << endl;
cout << "City... " << theFields[2] << endl;
cout << "State... " << theFields[3] << endl;
cout << "Zip Code... " << theFields[4] << endl;
} // end while
}//end read data

string * split(string theLine, char theDeliminator)
{
//Break theline into fields and save the fields to an array.
//Each field will occupy one element in a character array.
//theLine is a string with fields separated with theDeliminator character.
//Assumes the last field in the string is terminated with a newline.
//Useage: string *theFields = split(lineBuffer, ',');

//determine how many splits there will be so we can size our array
int splitCount = 0;
for(int i = 0; i < theLine.size(); i++)
{
if (theLine[i] == theDeliminator)
splitCount++;
}
splitCount++; //add one more to the count because there is not an ending comma

//create an array to hold the fields
string* theFieldArray;
theFieldArray = new string[splitCount];
//split the string into seperate fields
string theField = "";
int commaCount = 0;

for(int i = 0; i < theLine.size(); i++) //read each character and look for the deliminator
{
if (theLine[i] != theDeliminator)
{
theField += theLine[i]; //build the field
}
else
{
//the deliminator was hit so save to the field to the array
theFieldArray[commaCount] = theField; //save the field to the array
theField = "";
commaCount++;
}
}

theFieldArray[commaCount] = theField; //the last field is not marked with a comma...

return theFieldArray;
}//end split
First of all, please use code tags. See http://a.ly/5QH for usage details.

Second: In a debugger capable of executing one line at a time, execute your program (one line at a time). As you go, verify the variables, pointers, etc. so you ensure they contain the expected data as the program moves forward. Continue the process until the program crashes. At this point you'll know which line is the one causing the crash, and if you paid attention to variable values you probably know the reason of the crash.

If, after determining the line of the crash you are still unable to resolve, post an update here.
Topic archived. No new replies allowed.