C++ end of file error?

Hello, this is the error I am receiving.

My program is crashing on the default data:
> terminate called after throwing an instance of 'std::out_of_range'
> what(): basic_string::substr
> Aborted (core dumped)
I have no idea what is wrong with it, I need some help trying to find it.


It is in my main loop and is an end of file error.

So here is my main.

Thank you very much, I have been working on this for a very long time and am new to C++.
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

This program will calculate haul potential, the distance in standard units to the planet, and 
the planet with the highest resources(HP).  The closest and first encountered planet with the most
profit will be selected first.  

*/


#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <iomanip>
#include <cstring>

using namespace std;

void CheckTheFile(string & coor1, string & coor2, string & coor3,
		  string & value, int & base,
		  string & planetName, ifstream & inFile);

int ConvertBase(string convert, int base);

float TheDistance(int coordinateA, int coordinateB, int coordinateC);

float HP(int planetValue, float theDistance);




const int COORDA = 6;
const int COORDB = 16;
const int COORDC = 1911;


int main() {


  ifstream inFile;


  int base, 
      coordinateA, 
      coordinateB, 
      coordinateC, 
      planetValue, 
      newCoor1 = 0, 
      bestA = 0,
      bestB =0, 
      bestC = 0;

  float theDistance, 
        haul, 
        bestDistance, 
        bestHP = 0.0;
  
  string theFileName,
         coor1,
         coor2,
         coor3,
         planetName,
         bestPlanet,
         value;
	 
  char answer;

  bool verbose;


  cout << "Please enter the file name below: " << endl;
  cin >> theFileName;

  inFile.open(theFileName.c_str());
  
   
  
  cout << endl << "Processing... " << theFileName+":" 
       << endl << endl;
  
    
 while (answer != 'Y' && answer != 'N') {
   cout << "Enter (Y) or (N) for verbose mode " << endl;
       cin >> answer;
       // turns answer into uppercase  
       answer = toupper(answer);
 }
       if (answer == 'Y') {
          verbose = true; }
  
   
       else if (answer == 'N') { 
          verbose = false;
       }
  

  
  while (inFile) {
    CheckTheFile(coor1, coor2, coor3, value, base, planetName, inFile);
   
    coordinateA = ConvertBase(coor1, base);
    coordinateB = ConvertBase(coor2, base);
    coordinateC = ConvertBase(coor3, base);
    
    planetValue = ConvertBase(value, base);
    
    theDistance = TheDistance(coordinateA, coordinateB, coordinateC);

    haul = HP(planetValue, theDistance);

    if(haul >= bestHP) 
      {
	bestA = coordinateA;
	bestB = coordinateB;
	bestC = coordinateC;
	bestDistance = theDistance;
	bestHP = haul;
	bestPlanet = planetName;
      }   

 
    if (verbose) {
      cout << planetName << ", (" << coordinateA << ","
	   << coordinateB << ","
	   << coordinateC << ") " << fixed 
	   << setprecision(1) << theDistance 
	   << " " << fixed 
	   << setprecision(2)
	   << haul << endl;
    }
  }

  cout << endl << "IBM should mine " << endl << bestPlanet
       << ", (" << bestA << ","
       << bestB << "," << bestC << ") "
       << fixed 
       << setprecision(1) << bestDistance << " " << setprecision(2) 
       << bestHP << endl;


  inFile.close();
  return 0;
}
  
void CheckTheFile(string & coor1, string & coor2, string & coor3, 
		  string & value, int & base,
		  string & planetName, ifstream & inFile) {

  string coorLine,
         secondLine;

  int coordLen,
      thePara,
      theFirstComma,
      theSecondComma,
      theSecondLength;

  getline(inFile, coorLine);
  getline(inFile, secondLine);
  
  inFile >> base;
  
  coordLen = coorLine.length();
 
  theFirstComma = coorLine.find(",");
  coor1 = coorLine.substr(1, theFirstComma-1);
  
  coorLine = coorLine.substr(theFirstComma+1, coordLen);
  theSecondComma = coorLine.find(",");
  
  coor2 = coorLine.substr(0, theSecondComma);
  coorLine = coorLine.substr(theSecondComma+1, coordLen);
 
  thePara = coorLine.find(")");
  coor3 = coorLine.substr(0, thePara);
 
  theSecondLength = secondLine.length();
  theFirstComma = secondLine.find(",");
  
  
  value = secondLine.substr(0, theFirstComma);
  planetName = secondLine.substr(theFirstComma+2, theSecondLength);
  
  inFile.ignore();

  return;

}

int ConvertBase(string convert, int base) {

  int convertTheLength = convert.length(),
       newCoor = 0,
       exp = 0;

  
  for (int i =  convertTheLength - 1; i >= 0; i--) {
    
    switch(convert.at(i)) {
    case '0':
      
      newCoor +=0 * pow(base, exp);
      break;
      
    case '1':
      
      newCoor +=1 *pow(base, exp);
      break;
      
    case '2':

      newCoor+=2 *pow (base, exp);
      break;

    case '3':

      newCoor+=3 * pow (base, exp);
      break;

    case '4':
  
      newCoor+=4 * pow (base, exp);
      break;

    case '5':

      newCoor+=5 * pow (base, exp);
      break;
      
    case '6':

      newCoor+=6 * pow (base, exp);
      break;
      
    case '7':

      newCoor+=7 * pow (base, exp);
      break;

    case '8':

      newCoor+=8 * pow (base, exp);
      break;

    case '9':

      newCoor+=9 * pow (base, exp);
      break;

    case 'A':

      newCoor+=10 * pow (base, exp);
      break;

    case 'B':
      newCoor+=11 * pow (base, exp);
      break;

    case 'C':

      newCoor+=12 * pow (base, exp);
      break;

    case 'D':

      newCoor+= 13 * pow (base, exp);
      break;

    case 'E':
      
      newCoor+= 14 * pow (base, exp);
      break;

    case 'F':

      newCoor+= 15 * pow (base, exp);
      break;
    }
    exp++;
  }  // ends the statement and switch
  
 
 
  return newCoor;
}

float TheDistance(int coordinateA, int coordinateB, int coordinateC) {      

  float x,y,z,distance;

  x = coordinateA - COORDA;
  x = pow(x,2);

  y = coordinateB - COORDB;    //determines the distance
  y = pow(y,2);

  z = coordinateC - COORDC;
  z = pow(z,2);

  distance = sqrt(x+y+z);
  
  return distance;

}

float HP(int planetValue, float theDistance) {

  float haul;

  haul = planetValue/theDistance;

  return haul;
} 
      
      
  

  
}
Last edited on
Post more code. Several functions are used in this snippet.
Ok I have edited it and posted more code.

Thanks.
On the first glance, after getline at line 157 encounters the end of file, it sets the stream error indicator (which you don't check), coorLine is an empty string. .find() fails and returns npos, .substr() fails and throws the exception.
Try to remember where you learned to write "while(infile)" and avoid that like fire.
Yeah I don't know how to fix that while loop.
Topic archived. No new replies allowed.