Need Help Solving core dump

This program is supposed to read in text from a file, make specific changes to the text file, and then output the final results. The changes are baked into the program. The code compiles and asks the user for an input file. But once the user provides and input file, it produces a core dump. What am I doing wrong? How do I fix it?

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <iostream>
#include <fstream>    // required to read in from external file
#include <string>
using namespace std;

struct wordlist
{
  string word;
  string sentence;
};



/*****************************************************************************/
/*                                                                           */
/*   Function Prototypes                                                     */
/*   Description: Informs compiler about functions to be used in program.    */
/*                                                                           */
/*****************************************************************************/

bool openInputFile( ifstream &ifs );
void upload_data( ifstream &ifs, wordlist wordArray[], int &count );
void replace( wordlist wordArray[], int &count );
void remove( wordlist wordArray[], int &count );
void swap( wordlist wordArray[], int &count );
void print_Results( wordlist wordArray[], int &count );



/*****************************************************************************/
/*                                                                           */
/*   Function name: MAIN                                                     */
/*   Description: Triggers all of the functions in the order listed          */
/*   Parameters:     none                                                    */
/*   Return value:   none                                                    */
/*                                                                           */
/*****************************************************************************/

int main()
{
  int count = 30;    // there are 30 words in  data file, it will never increase
  ifstream ifs;
  wordlist wordArray[30];
  //bool begin = openInputFile( ifs );
  if(openInputFile(ifs))
    {
      upload_data( ifs, wordArray, count );
    }
  //replace( wordArray, count );
  //remove( wordArray, count );
  //swap( wordArray, count );
  print_Results( wordArray, count );

  return 0;
}



/*****************************************************************************/
/*                                                                           */
/*   Function name: openInputFile                                            */
/*   Description: Reads in the data from an external file (strings)          */
/*   Parameters:     none                                                    */
/*   Return value:   none                                                    */
/*                                                                           */
/*****************************************************************************/

bool openInputFile( ifstream &ifs )
{
  string fileName;
  cout << "Please enter the name of the data file: ";
  cin >> fileName;
  cout << endl;

  ifs.open( fileName.c_str() );
  if( ifs.fail() )
    {
      cout << "Error opening input file. This file was not located in the direc\
tory. " << '\n';
      return( false );
    }
  cout << "File opened successfully" << endl;
  return( true );
}



/*****************************************************************************/
/*                                                                           */
/*   Function name: upload_data                                              */
/*   Description: Uploads data from an external file (strings) into struct   */
/*   Parameters:     none                                                    */
/*   Return value:   none                                                    */
/*                                                                           */
/*****************************************************************************/

void upload_data( ifstream &inf, wordlist wordArray[], int &linecount )
{
  int count = 0;
  string words;
  string line;
  int wordcount= 0;   // changed from string to int

  while(!inf.eof())
    {
      getline(inf,line);
      wordArray[linecount].sentence = line;

      // .word after [count] ensures that data is read in through struct
      linecount++;                        // counter keeps going until it reach\
es 30
      //cout << wordArray[count].word;
    }
  while(!inf.eof())
    {
      wordArray[wordcount].word = words;
      wordcount++;
    }
}



/*****************************************************************************/
/*                                                                           */
/*   Function name: replace                                                  */
/*   Description: Replaces a select keyword from the text with another word  */
/*   Parameters:     none                                                    */
/*   Return value:   none                                                    */
/*                                                                           */
/*****************************************************************************/

void replace( wordlist wordArray[], int &count )
{
  int index = 0;
  int counter = 0;

  for( int i = 0; i < count; i++ )
    {
      if( wordArray[i].word == "blank-N" )
        {
          index = i;
          counter++;
        }

      if( counter == 2 )
        {
          wordArray[index].word == "friend";
        }
    }

/*****************************************************************************/
/*                                                                           */
/*   Function name: remove                                                   */
/*   Description: Removes a select keyword from the text                     */
/*   Parameters:     none                                                    */
/*   Return value:   none                                                    */
/*                                                                           */
/*****************************************************************************/

void remove( wordlist wordArray[], int &count )
{
  for( int i = 0; i < count; i++ )
    {
      if( wordArray[i].word == "blank-D" )
        {
          wordArray[i].word == "";
        }
    }
}



/*****************************************************************************/
/*                                                                           */
/*   Function name: swap                                                     */
/*   Description: Move the 3rd line of text so it becomes the first line     */
/*   Parameters:     none                                                    */
/*   Return value:   none                                                    */
/*                                                                           */
/*****************************************************************************/

void swap( wordlist wordArray[], int &count )
{
  string line1;
  string line3;
  int indexOfLine1;
  int indexOfLine3;
  for ( int i = 0; i < count; i++ )
    {
      if( wordArray[i].sentence == "Be kind to your blank-A blank-N everyday " \
)
        {
          wordArray[i].sentence == line3;
          indexOfLine1 = i;
        }

      if( wordArray[i].word == "Be kind to your blank-N -footed blank-D" )
        {
          wordArray[i].sentence == line1;//"Be kind to your blank-A blank-N eve\
ryday" << endl << "Be kind to your blank-N -footed blank-D" << endl;
          indexOfLine3 = i;
        }

    }
  string temp = line1; //move line 1 into temp
  wordArray[indexOfLine1].sentence = line3;
  wordArray[indexOfLine3].sentence = temp;
}



/*****************************************************************************/
/*                                                                           */
/*   Function name: print_Results                                            */
/*   Description: Outputs the current text after the modifications           */
/*   Parameters:     none                                                    */
/*   Return value:   none                                                    */
/*                                                                           */
/*****************************************************************************/

void print_Results( wordlist wordArray[], int &count )
{
  for ( int i = 0; i < count; i++ )
    {
      cout << wordArray[i].sentence << endl;
      /*if(wordArray[i].word == "")
        {
        cout << endl;
        }*/
    }
} 
https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong

Possible that you read one line too much and that accesses the array out of bounds.

Is it really necessary to use arrays? Would be easier with std::vector;
I just read the stackoverflow link you posted and I'm not following how it relates to my program.

Since this program is a school assignment we are supposed to either use arrays or linked lists. We never touched upon vectors yet.
You set count to 30 on line 41.

You passed this as a reference parameter to routine upload_data() on line 47, where it becomes variable linecount (still with value 30).

You then proceed to try to set element linecount (i.e. 30) of wordArray[] on line 107. This is one beyond the end of the array ([0] to [29]).

You then increment linecount each time through the list ... writing even further beyond the end of the array.

So there is little surprise that your program is unhappy!


There is quite a lot wrong with your routine upload_data(), not least because you seem to have got very confused between linecount (passed from a previous variable count in another routine), a separate local variable called count, and a separate wordcount, which does goodness knows what.

I should have a good look at the link @Thomas1965 gave you as you try to loop on the basis of eof twice in this routine. It is highly unreliable because reading the last data item isn't guaranteed to set the eof bit.
Topic archived. No new replies allowed.