Help with code please


I am trying to get this function to work. It will display:
Name......
Street....
City......

Then terminate and I just can't seem to get it right. Thanks for helping! :-)






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void readData(void)
{
	ofstream outMyStream;
	outMyStream.open ("c:/TestAddress.txt");
		if (outMyStream.is_open());
    ifstream inMyStream (FileName, ios::in);
    string firstline;
	inMyStream >> firstline;
    while (!inMyStream.eof() )
    {
        getline (inMyStream, firstline, '\n');
		cout << endl;
        string *theFields = split(firstline, ',');
        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 read data 




Here is the whole program ... I have used help from others to get this put together, so if you notice anything else I may have messed up please let me know. Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#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[] = "c:/TestAddress.txt";
  
int main ()
{
    menu();
    return 0;
} //end main
  
void menu(void)
{   
    char choice = ' ';
    cout << "\nWhat Would You Like To Do With These Records: \n\n";
    cout << "Append Records (A), Show Records (S), or Exit (E)\n"; 
     cin >> choice;
  
    while(choice == 'A' || choice == 'S')
    {
        switch(choice)
        {
            case 'a':
            case 'A':
                writeData();
                break;
            case 's':
            case 'S':
                readData();
                break;
        }
        cout << "What Else Would You Like To Do?\n";
        cout << "Append Records (A), Show Records (S), or Exit (E)\n"; 
         cin >> choice;
    }
}//end menu
  
void writeData(void)
{
    char choice = ' ';
    string name = "";
    string street = "";
    string city = "";
    string state = "";
    string zipCode = "";
    ofstream outMyStream(FileName, ios::app);
  
    do
    {cin.ignore();
        cout << "\nEnter The Name: ";
        getline(cin, name);
        cout << "\nEnter The Street: ";
        getline(cin, street);
        cout << "\nEnter The City: "; 
        getline(cin, city); 
        cout << "\nEnter The State: "; 
        getline(cin, state);
        cout << "\nEnter The Zip Code: "; 
        getline(cin, zipCode);
  
        outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;
  
        cout << "\nEnter another Record? (Y/N) ";
        cin >> choice;
    }
    while (choice == 'Y' || choice == 'Y' ); 
    outMyStream.close();
}//end write data
  
void readData(void)
{
	ofstream outMyStream;
	outMyStream.open ("c:/TestAddress.txt");
		if (outMyStream.is_open());
    ifstream inMyStream (FileName, ios::in);
    string firstline;
	inMyStream >> firstline;
    while (!inMyStream.eof() )
    {
        getline (inMyStream, firstline, '\n');
		cout << endl;
        string *theFields = split(firstline, ',');
        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 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 
1
2
3
4
5
6
7
8
9
10
11
12
void readData(void)
{
	ofstream outMyStream;
	outMyStream.open ("c:/TestAddress.txt"); // this is making output file (lines you PUT in)
		if (outMyStream.is_open());  //if output file is open do what? in general almost always ofstream is ok (write permissions?)
    ifstream inMyStream (FileName, ios::in); // input and output files are the same ehh?
    string firstline;
	inMyStream >> firstline;
.
.
.
}//end read data  
input and output files are the same ehh?

what?!?

I don't understand what to do...can you elaborate?
In line 11 of the original code you wrote:
const char FileName[] = "c:/TestAddress.txt";

After that on function readData you opened INPUT stream to use FileName variable as file name and OUTPUT stream to use same file name: (line 80)
outMyStream.open ("c:/TestAddress.txt");

Do you ABSOLUTELY trust that each input line contains 5 "fields"? (lines 90-94)


BTW; you used new-operator on line 118. Where is delete-operator?


Topic archived. No new replies allowed.