Homework help with split and delimiter

So, I posted a bit earlier in the week about this piece. I got the regular errors out of the way I think. But now, I'm having some problems understanding what the maker of the assignment wants. it wasnt made by my instructor, it was made by someone else who designed the course. Please I know it's long, but I really dont understand the last part with the string *split(...) T-T
also, i kind of get what a delimiter is, but just some clarification on what it is please. Thanks
Edit: wrong assignment so scratch the first sentence about previous postings. gettin frazzled. @_@

Here's what I have and then what the instructions say:

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#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;
}

//------------------------------------------------------------------------------

void menu(void)
{
	char input;
	
	cout<<"-----Welcome to the menu!-----"<<endl;
	cout<<"\n(A)ppend Records"<<endl;
	cout<<"(S)how Records"<<endl;
	cout<<"(E)xit"<<endl;
	cin>>input;

	if(input=='A'||input=='a')
	{
		writeData();
	}

	if(input=='S'||input=='s')
	{
		readData();
	}
	if(input=='E'||input=='e')
	{
		return;
	}
}

//--------------------------------------------------------------------------------------------------------

void writeData(void)
{
	ofstream outFile;
	string name, street, city, state, zip;
	char choice;
	outFile.open("TestAddress.txt", ios::app);
	do
	{
		cout<<"Please enter the name: ";
		getline(cin, name);
		cout<<"Please enter the street: ";
		getline(cin, street);
		cout<<"Please enter the city: ";
		getline(cin, city);
		cout<<"Please enter the state: ";
		getline(cin, state);
		cout<<"Please enter the zip code: ";
		getline(cin, zip);

		outFile<<name<<", "<<street<<", "<<city<<", "<<state<<", "<<zip<<endl;

		cout<<"Enter another record? (Y/N): ";
		cin>>choice;
		if(choice != 'y' || choice != 'Y' || choice != 'n' || choice != 'N')
		{
			cout<<"Error... You must enter a 'Y' or 'N'"<<endl;
			cout<<"Enter another record? (Y/N): ";
			cin>>choice;
		}
	}
		while(choice!='Y' || choice!='y');
		outFile.close();
}

//-----------------------------------------------------------------------------------------

void readData(void)
{
	string line;
	ifstream inFile("TestAddress.txt");
	
	if(inFile.is_open())
	{
		int count=1;
		getline(inFile,line);
		
		while(!inFile.eof())
		{
			cout<<count<<line<<endl;
			count++;
			getline(inFile,line);
		}
		inFile.close();
	}
	
	else cout<<"Error opening file."<<endl;
	string *split(string line, char delimiter);
}

//-------------------------------------------------------------------------------------------------------


//what is this for?? I dont understand.
string * split(string theLine, char theDeliminator)
{

	int splitCount=0;
	for(int i=0; i<theLine.size(); i++)
	{
		if(theLine[i] == theDeliminator)
		{
			splitCount++;
		}
	}
	splitCount++;
}

//------------------------------------------------------------------------------------------


//or this?? What is THIS for?? I dont understand. I know it's a pointer and such,
string * theFieldArray;
theFieldArray = new string[splitcount];

string theField="";
int commaCount=0;

for(int i=0; i<theLine.size(); i++)
{
	if(theLine[i] != theDeliminator)
	{
		theField += theLine[i];
	}
	else 
	{
		theFieldArray[commaCount] = theField;
		theField="";
		commaCount++;
	}
}
theFieldArray[commaCount] = theField;

return theFieldArray;

	}

}



//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) {
//allow user to choose to append records, display records or exit the program
}//end menu
void writeData(void){
//Write the Address Info to a file
}//end write data
void readData(void){
//read data from a file
//use the split function to break a
//deliminated line of text into fields
}//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

Last edited on
Is that the full assignment? There seems to be a lot left out.

Where is readData() supposed to put the data once it's read? Where is writeData() supposed to get its data from?

A delimiter separates something into parts. In this case, the delimiter is a character that separates a line of text into fields. For example, if you have a line like:
I,love,to,program
And the delimiter is a comma, then it indicates that the line has 4 fields: "I", "love", "to", and "program". The split() function takes a line and the delimiter, and returns an array of strings where each string contains one field. The array is allocated on the heap and so the caller has to delete it.

That split() function is poorly designed. If you call it, then how many fields get returned in the array? There's no way to know.
Topic archived. No new replies allowed.