Ok, I am confused by this. I am doing a school project on i/o with an address database. The professor gave us a template to work with, which saved me a few minutes.
The problem is I can enter information to the txt file fine. When I attempted to read it I get this error from VS2010 "Unhandled exception at 0x00cb3246 in LAB7.exe: 0xC0000005: Access violation reading location 0x00000014." Can someone take a look at my code and give me some direction?

//Specification: Append and display records in an address database 

// Headers
#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/TestAddress.txt";
ifstream myAddress("TestAddress.txt");

    string name = " ";
    string address = " ";
    string street= " ";
    string city = " ";
    string state = " ";
    string zipCode = " ";
    int record = 0;
    ofstream outMyAddress("TestAddress.txt", ios::out);

int main ()

    return 0;
} //end main

void menu(void)
    //allow user to choose to append records, display records or exit the program
    char userChoice = ' ';

    //Display Menu
    cout << "\nName and Address database:" << endl;  
    cout << endl;
    cout << "\n\n(A)ppend record, (S)how Record, (E)xit:";
        cin >> userChoice;
        //Users Choice

    switch (userChoice)
        case 'a':
        case 'A'://Append Record
            myAddress.open (FileName, ios::app);
            if (myAddress.is_open())
                {writeData(); }

        case 's':
        case 'S'://Show record
            myAddress.open ("TestAddress.txt", ios:: in);
            if (myAddress.is_open())
                {readData(); }


        case 'e':
        case 'E'://Exit

            cout << "Invalid choice" << endl;
            cout << endl << endl << endl;



}//end menu

void writeData(void) //Write the Address Info to a file

    char answer = ' ';
    char response = ' ';

       //entering loop
    while (answer != 'n' || answer != 'N')

        cout << endl;
        getline(cin, name);
        cout<< "\nEnter name: ";
        getline(cin, name);
        cout<< "\nEnter Street: ";
        getline(cin, street);
        cout<< "\nEnter City: ";
        getline(cin, city);
        cout<< "\nEnter State: ";
        getline(cin, state);
        cout<< "\nEnter Zip Code: ";
        getline(cin, zipCode);
        cout << endl;

        outMyAddress << name << ", " << street << " ," << city << ", " << state << " ," << zipCode << endl;

        cout << "\nWould you like to enter another record? (Y/N)" << endl;
        cin >> response;

     if (response == 'n' || response == 'N')
        return menu();


}//end write data

void readData(void)
    ifstream inMyAddress("TestAddress.txt", ios::in);
    string firstLine;
    inMyAddress >> firstLine;
    getline (myAddress, firstLine, '\n'); 

                cout << endl;
                cout << "Reading the file(s)..." << endl;
                cout << endl;

                //read data from a file                             
                //use the split function to break a deliminated line of text into fields    

                cout << "Record #: " << record << endl;
                string *theField = split(firstLine, ',');
                cout << "Name......" << theField[0] << endl;
                cout << "Street......" << theField[1] << endl;
                cout << "City......" << theField[2] << endl;
                cout << "State......" << theField[3] << endl;
                cout << "Zip Code......" << theField[4] << endl;

                return menu();

}//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)
    return 0;

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 = "";
    theFieldArray[commaCount] = theField; //the last field is not marked with a comma...

    return theFieldArray;
} //end split
6 Years
Discussion Span
Last Post by np complete

In line number 37, 84, 85, you are declaring char userChoice= ' '. Instead try this char userChoice.
In line 94, you have a getline(cin, name); which I think should't be there.
Instead of spliting, try using ',' as delimiter in getline (myAddress, firstLine, '\n').
It will take all character that are before ','. Try out these first. I don't think that a separate spliting function is required.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.