#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Exclamation Sequential Files - Beginner Help


    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    
    void menu();
    void writeData();
    void readData();
    string * split(string, char);
    
    
    const char delimiter = ',';
    int main()
    {
    cout << "Welcome to the Records Program" << endl;
    menu();
    system("pause");
    return 0;
    }
    void menu()
    {
    char choice;
    do{
    cout << "(A)ppend Records" << endl;
    cout << "(S)how Records"<< endl;
    cout << "(E)xit" << endl;
    cout << "Enter choice: ";
    cin >> choice;
    cin.ignore();
    cout << endl;
    switch(toupper(choice))
    {
    case 'A': writeData(); break;
    case 'S': readData(); break;
    case 'E': cout << "Good-Bye" << endl; break;
    default: cout << "Invalid entry" << endl; break;
    }//swtich
    cout << endl;
    }while(choice != 'e' && choice != 'E');
    }
    
    
    void writeData()
    {
    ofstream outMyStream("myrecords.txt", ios::app);//open my text file
    if(outMyStream.is_open())
    {
    	char choice = ' ';
    	string name = "";
    	string street = "";
    	string city = "";
    	string state = "";
    	string zip = "";
    	char again = 'y';
    
    	do
    	{
    // get info
    	cout << "Enter name: ";
    	getline(cin, name);
    	cout << "Enter street: ";
    	getline(cin, street);
    	cout << "Enter city: ";
    	getline(cin, city);
    	cout << "Enter state: ";
    	getline(cin, state);
    	cout << "Enter Zip ";
    	cin >> zip;
    // write to the file
    	outMyStream << name << delimiter;
    	outMyStream << street << delimiter;
    	outMyStream << city << delimiter;
    	outMyStream << state << delimiter;
    	outMyStream << zip <<delimiter;
    
    	cout << "Enter another item? (Y or N) ";
    	cin >> again;
    	cin.ignore();
    	cout << endl;
    }while (again == 'y' || again == 'Y');
    outMyStream.close();
    }
    else
    cout << "File not opened" << endl;
    }//end write data
    
    
    void readData(void)
    {
        ifstream inMyStream("myrecords.txt");
    	string recBreaks = "";
    	recBreaks.assign(20, '-'); //This is the character between record displays
    	int itemCount = 1;
    cout << " Show Records " << endl;
    cout << recBreaks << endl;
    
        string lineBuffer;
        while (!inMyStream.eof() )
        {
            getline (inMyStream, lineBuffer, '\n');
            string *theFields = split(lineBuffer, ',');
    		cout << "Record # " << itemCount << endl;
            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;
        }
    cout << recBreaks << endl;
    itemCount++;
    
    
    }//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

    So my question here is that I am trying to get it to read multiple records and output them. When I run this program and I append say two records I will only get the output for the first record. I'd like to know where I am going wrong in regards to why it won't show any of the other "records" that I write to my file.

    It should output something like this (but I only get Record #1)

    Show Records
    __________________________________________
    Record #1
    Name...........John Smith
    Street..........902 Union Ave
    City.............Any Town
    State...........TX
    Zip Code......78552
    __________________________________________
    Record #2
    Name...........Eric Jones
    Street..........345 State Way
    City.............Fresno
    State...........CA
    Zip Code.......93432
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    [solved]


    Hello Everyone,

    I know I didn't give much time for people to answer but I actually took a step back from the computer and came back to figure out my issue. Turns out it was the way that my input was being written to the file. This is what it was doing when I was writing to "myRecords.txt"

    name, street, city, state, zip, name, street, city, state, zip,

    This was the reason I was only getting the first output because it stopped reading after the first zip. I corrected this by doing the following:

    Code:
    // write to the file
    	outMyStream << name << delimiter;
    	outMyStream << street << delimiter;
    	outMyStream << city << delimiter;
    	outMyStream << state << delimiter;
    	outMyStream << zip <<endl;
    Upon reviewing my code I posted on here I also noticed that I posted code that had the itemCount and line buffer incorrect in my readData() function. If anyone would like to know how I fixed it feel free to PM me.


    Got to love programming even those sometimes you just

IMN logo majestic logo threadwatch logo seochat tools logo