I am having a error and I can't find the problem

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//Specification: Append and display records in a address database 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

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

const char FileName[] = "TestAddress.txt";
const char delimiter = '#';

int main () 
{
        menu();
        return 0;
} //end main
void menu(void) 
{//allow user to choose to append records, display records or exit the program
	char choice;
	do
	{
		cout << "(A)ppend your records." << endl;
		cout << "(D)isplay your records." << endl;
		cout << "(E)xit the program." << endl;
		cout << "What would you like to do?";
		cin >> choice;
		cin.ignore();
		cout << endl;
		switch(toupper(choice))
		{
		case 'A': writeData();
					  break;
		case 'D': readData();
					  break;
		case 'E': cout << "Good-bye!!" << endl;
					  break;
		default: cout << "Invalid entry" << endl;
					 break;
		}//switch statement end
		cout << endl;
	}while ((choice != 'e') && (choice != 'E'));

}//end menu
void writeData(void)
{
	//Write the Address Info to a file
	ofstream OutStream (FileName, ios::app);
	if (OutStream.is_open())
	{
		string Name = " ";
		string Street = " ";
		string City = " ";
		string State = " ";
		string Zipcode = " ";
		char OK = 'Y';
		do
		{
			//get file data
			cout << "Name\n" << endl;
			getline(cin, Name);
			cout << "Street\n" << endl;
			getline(cin, Street);
			cout << "City\n" << endl;
			getline(cin, City);
			cout << "State\n" << endl;
			getline(cin, State);
			cout << "Zipcode\n" << endl;
			getline(cin, Zipcode);

			//write data to file
			OutStream << Name << delimiter;
			OutStream << Street << delimiter;
			OutStream << City << delimiter;
			OutStream << State << delimiter;
			OutStream << Zipcode << endl;

			cout << "Would you like to enter another record> (Y/N) ";
			cin >> OK;
			cin.ignore();


		}while (( OK == 'Y') || ( OK == 'y'));
		OutStream.close();
	}
	else
		cout << "File Error: Open Failed" << endl;


}//end write data
void readData(void)
{
	//read data from a file
	ifstream inMyStream(FileName, ios::in);
	if (inMyStream.is_open())
	{
		string recBreaks = "_";
		recBreaks.assign(20, "_");
		cout << "Show Records" << endl;

		string buffer;
		getline ( inMyStream, buffer, '\n');
		while (!inMyStream.eof())
		{
			string * theFields = split(buffer, delimiter);

			cout << recBreaks << endl;
			cout << "Name";
			cout.width(10);
			cout.fill('.');
			cout << theFields[0] << endl;
			cout << "Street";
			cout.width(10);
			cout.fill('.');
			cout << theFields[1] << endl;
			cout << "City";
			cout.width(10);
			cout.fill('.');
			cout << theFields[2] << endl;
			cout << "State";
			cout.width(10);
			cout.fill('.');
			cout << theFields[3] << endl;
			cout << "Zip Code";
			cout.width(10);
			cout.fill('.');
			cout << theFields[4] << endl;
			getline (inMyStream, buffer, '\n');
		}//end-of-loop
		cout << recBreaks << endl;
		inMyStream.close();
	}//end-of-if
	else
		cout << "File Error: Printing failed" << 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 < (int)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 < (int)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 



I am getting the error "Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast" I have asked my professor for help, however I haven't heard anything back. It's finals weeks so i'm sure she is busy. I have looked through the program, but honestly I don't know what is giving me the error. We (the students of this class) was given the "string * split(string theLine, char theDeliminator)" function. I have tried to look through it to find the error. I'm pretty sure that is where the error is happening because as I worked through the other function I didn't get this error until i wrote the "void readData(void)", which calls the "string * split(string theLine, char theDeliminator)" function.

I have never used to cout.width() or cout.fil() before, I just looked on google how to format my output so that I can create a "." line that extends from "Name" to the output(if that makes sense). Without hard coding it.
I have looked through the program, but honestly I don't know what is giving me the error.

When you compile the program and the compiler generates an error it will tell you on which line of what file the error occurred. I would suggest looking there.

I'm going to guess line 101. Maybe you should take another look at what parameters assign takes. Hint: " " is not a space character. It's a string.
I was looking at this "http://www.cplusplus.com/reference/string/string/assign/"

I that it has this example:


str.assign(10,'*');
cout << str << endl; // "**********"



And I thought i did basically the same thing, I just changed the variable names:


string recBreaks = '-';
recBreaks.assign(20, '-');
Except that's not what you're doing in line 101.

" " is not ' '

The first is a c-string consisting of a space and a nul character. The second is a single character.
Last edited on
Topic archived. No new replies allowed.