Text file reading program's subprogram producing error

I seem to be having trouble with my program coming up with an unexpected error. I'm making a program that can parse words from a line into substrings into a 10 element array struct. (I think I said that right)

My program reads through citizeninformation_parse.txt, which is a text file containing information of 10 people, where all of the substrings I want to chunk out are separated by commas. My goal is to output the information in an organized fashion, like

#0
Joe Johnson
123 Fake Street
Baltimore, MD 21075
Phone: 410-555-5555
Income: $100,000
Political party: Republican

citizeninformation_parse.txt
Joe, Alice, Jan, Oliver, Bob, Wade, Alice, Georgina, Quinn, Lindsay
Johnson, Warsong, Duran, Jackson, Joe, Miller, Deveaunx, Lopez, Belathasar, Lohan
123 Fake Street, 234 Notreal Avenue, 345 Almosttrue Court, 456 Couldashoula Road, 567 Redneck Way, 678 Bellring Street, 789 Wonderland Drive, 890 La Costa Avenue, 123 Glee Street, 9110 S Central Ave
Baltimore, Delaware City, Juneaeu, Sacramento, Centerville, Tampa, Oklahoma City, Farmington, San Francisco, Los Angeles
MD, DE, AK, CA, AR, FL, OK, NM, CA, CA
21075, 19707, 99801, 94203, 72829, 337601, 73101, 87401, 94101, 90001
410-555-5555, 302-111-1111, 907-333-3333, 916-222-2222, 479-505-5050, 813-444-4444, 405-123-4567, 505-909-1234, 415-123-4567, 323-555-5555
100,000, 251,123, 65,000, 235,000, 23,001, 325,000, 125,000, 500,000, 250,000, 2
Republican, Democrat, Democrat, Republican, Republican, Democrat, Republican, Independent, Independent, N/A


So the first line is first name, second is last name, address, etc.

I finally made a function that goes through every character in the line from the file input, and if the next character is a comma, then it stores the substring, blah blah, and it supposively works correctly. Here's the function for first name (the first line of citizeninformation_parse.txt)

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
void firstNameParse (Info person[9], string line)
{
  int index = 0; // start with 0 in the array
  int length = line.length(); // parse length of the string from file
  int firstCharacterPosition = 0;
  int nextCharacterPosition = 0;
  int parseLength = 0;
  char nextCharacter;

  for (int i = 0; length > i; i++)
  {
   parseLength++;
   nextCharacterPosition = i + 1;
   nextCharacter = line[nextCharacterPosition];

   if (nextCharacter == ',')
   {
    person[index].firstname = line.substr (firstCharacterPosition,parseLength);
    firstCharacterPosition = nextCharacterPosition + 2; // skips comma and space
    index++;
    parseLength = -2; // reset
   } // cycles through the position of the first character, length of parse, adds 1
     // to the index, and then sets the parseLength to -2 to skip the comma/space

   if (nextCharacterPosition == length)
   {
    person[index].firstname = line.substr (firstCharacterPosition,parseLength);
   } // to parse the last name in the line
  }

}


So I figured since I had a working formula, I could just cut-and-paste the function 9 more times, and simply just changing the struct value of person[i].

It worked for first name, and it worked for last name. However, when I got to address, I came across an error.

http://i53.tinypic.com/fm6jcw.png (screenshot of error)

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
void addressParse (Info person[9], string line)
{
  int index = 0;
  int length = line.length();
  int firstCharacterPosition = 0;
  int nextCharacterPosition = 0;
  int parseLength = 0;
  char nextCharacter;

  for (int i = 0; length > i; i++)
  {
   parseLength++;
   nextCharacterPosition = i + 1;
   nextCharacter = line[nextCharacterPosition];

   if (nextCharacter == ',')
   {
    person[index].address = line.substr (firstCharacterPosition,parseLength);
    firstCharacterPosition = nextCharacterPosition + 2; // skips comma and space
    index++;
    parseLength = -2; // reset
   }

   if (nextCharacterPosition == length)
   {
    person[index].address = line.substr (firstCharacterPosition,parseLength);
   }
  }

}


The function stored the address values, but threw some weird error.

Just to confirm, I'm compiling with Microsoft VS2010. What I'm looking for isn't necessarily a code clean-up; I know that my coding-style is probably imperfect, but I'm still a student trying to learn the basics. What I'm looking for is why am I getting an error at the address function? The second post will contain my full script, but I have commented out the rest of the functionality besides the firstName, lastName, and address parse functions.

Any help would be appreciated.
textfileparser.cpp:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Info {
  string firstname;
  string lastname;
  string address;
  string city;
  string state;
  string zip;
  string phone;
  string income;
  string politicalparty;
};

string inputTextFile (string iFileName, string default_text)
{
  cout << "Please input the text file name. (default file: " << default_text << ")" << endl;
  cout << "File name: ";
  getline (cin, iFileName);
  if (iFileName.length() == 0) iFileName = default_text;
  cout << endl;
  return iFileName;
}

void firstNameParse (Info person[9], string line)
{
  int index = 0; // start with 0 in the array
  int length = line.length(); // parse length of the string from file
  int firstCharacterPosition = 0;
  int nextCharacterPosition = 0;
  int parseLength = 0;
  char nextCharacter;

  for (int i = 0; length > i; i++)
  {
   parseLength++;
   nextCharacterPosition = i + 1;
   nextCharacter = line[nextCharacterPosition];

   if (nextCharacter == ',')
   {
    person[index].firstname = line.substr (firstCharacterPosition,parseLength);
    firstCharacterPosition = nextCharacterPosition + 2; // skips comma and space
    index++;
    parseLength = -2; // reset
   } // cycles through the position of the first character, length of parse, adds 1
     // to the index, and then sets the parseLength to -2 to skip the comma/space

   if (nextCharacterPosition == length)
   {
    person[index].firstname = line.substr (firstCharacterPosition,parseLength);
   } // to parse the last name in the line
  }

}

void lastNameParse (Info person[9], string line)
{
  int index = 0;
  int length = line.length();
  int firstCharacterPosition = 0;
  int nextCharacterPosition = 0;
  int parseLength = 0;
  char nextCharacter;

  for (int i = 0; length > i; i++)
  {
   parseLength++;
   nextCharacterPosition = i + 1;
   nextCharacter = line[nextCharacterPosition];

   if (nextCharacter == ',')
   {
    person[index].lastname = line.substr (firstCharacterPosition,parseLength);
    firstCharacterPosition = nextCharacterPosition + 2; // skips comma and space
    index++;
    parseLength = -2; // reset
   }

   if (nextCharacterPosition == length)
   {
    person[index].lastname = line.substr (firstCharacterPosition,parseLength);
   }
  }

}

void addressParse (Info person[9], string line)
{
  int index = 0;
  int length = line.length();
  int firstCharacterPosition = 0;
  int nextCharacterPosition = 0;
  int parseLength = 0;
  char nextCharacter;

  for (int i = 0; length > i; i++)
  {
   parseLength++;
   nextCharacterPosition = i + 1;
   nextCharacter = line[nextCharacterPosition];

   if (nextCharacter == ',')
   {
    person[index].address = line.substr (firstCharacterPosition,parseLength);
    firstCharacterPosition = nextCharacterPosition + 2; // skips comma and space
    index++;
    parseLength = -2; // reset
   }

   if (nextCharacterPosition == length)
   {
    person[index].address = line.substr (firstCharacterPosition,parseLength);
   }
  }

}

int main ()
{
  int option = 0; // for parseLine function
  string fileLine;
  string default_txt = "citizeninformation_parse.txt";
  string inputFileName;
  int numberOfLetters = 10;
  Info person[9]; // create 10-slot array, 1 for each person, start with 0.

file_input:
  inputFileName = inputTextFile(inputFileName, default_txt);

fin_initiate:
  ifstream fin;
  fin.open(inputFileName.c_str());
  if (!fin.good()) throw "I/O error";

   getline(fin, fileLine);
   firstNameParse(person, fileLine);
   getline(fin, fileLine);
   lastNameParse(person, fileLine);
   getline(fin, fileLine);
   addressParse(person, fileLine);

for (int i = 0; i < 10; i++)
{
 cout << "#" << i << ": " << person[i].firstname << ' ' << person[i].lastname << endl;
 cout << person[i].address << endl;
 cout << endl;
}

close:
  fin.close();

  return 0;
}
1
2
3
nextCharacterPosition = i + 1;//what if NCP == length here
nextCharacter = line[nextCharacterPosition]; //out of bounds (crash)
if (nextCharacterPosition == length)
Last edited on
ediagas wrote:
So I figured since I had a working formula, I could just cut-and-paste the function 9 more times, and simply just changing the struct value of person[i].

So what is the purpose of having a function in the first place?
Try to abstract a little more. Make a tokenize function, make another that fill the array.
You could use a set function with an argument that indicates which atributte will be set, or pointers to member function, or to class atributtes (if they are public ).
So everithing reduces to:
1
2
3
4
magick(person, fileLine, FNAME);
magick(person, fileLine, LNAME);
magick(person, fileLine, ADDRESS);
magick(person, fileLine, PARTY);


PS: you may want to read about string::find() and strtok()
Last edited on
Topic archived. No new replies allowed.