1

So I'm unable to create a substring cut using ranges. I am making an airport program where you feed the program a txt.file and it has to divide the lines I get from it into different strings. For instance, I have the following text data:

CL903 LONDON 41000 14.35 08906 //number of flight, destination, price, etc.

UQ5723 SYDNEY 53090 23.20 12986

IC5984 TORONTO 18030 04.45 03260

AM608 TOKYO 41070 18.45 11315

so the first string will be on the lines of this (variables are in Spanish):

numVuelo[n] = M[n].substr(0,5)

this line will work perfectly, but when I move to the next one (from 7 to 14), it tells me that it's out of range, even though It's between the 0 and 31st values of the length of the string.

M[n] gets all of the strings on the text, I'm using Codeblocks and a class style with header and all. I'll copy the code below...

This is my header Vuelo.h:

#ifndef VUELO_H
#define VUELO_H
#include <iostream>
#include <fstream>
#include <string>
#define NUM_FLIGHTS 10

using namespace std;

class Vuelo
{
    public:
        Vuelo(int N);
        virtual ~Vuelo();

        void setM();
        void setNumVuelo(string _numVuelo, int n);
        void setDestino(string _destino, int n);
        void setPrecio(string _precio, int n);


    private:
        string M[NUM_FLIGHTS];
        string numVuelo[NUM_FLIGHTS];
        string destino[NUM_FLIGHTS+1]; //somehow "destino" doesn't work without the +1 but everything else does
        float precio[NUM_FLIGHTS];

Then, on another code called Vuelo.cpp I have the following

#include "Vuelo.h"

Vuelo::Vuelo(int N)
{
    M[N] = { };
    numVuelo[N] = { };
    destino[N] = { };
    precio[N] = { };
}
Vuelo::~Vuelo()
{
    //nope
}
void Vuelo::setM()
{
    int c = 1;
    string s;
    ifstream F ("flights.txt");
    if(F.is_open())
    {
        while (!F.eof())
        {
            getline(F,s);
            M[c] = s;
            cout << M[c] << endl;
            c++;
        }
        //sets all values
    for(c = 0; c < NUM_FLIGHTS; c++)
    {
        setNumVuelo(M[c],c);
        setDestino(M[c],c);
        setPrecio(M[c],c);
    }
    F.close();
    }
    else
    {
        cout << "ERROR document wasn't found" << endl;
    }
}

void Vuelo::setNumVuelo(string _numVuelo, int n)
{
    numVuelo[n]= _numVuelo.substr(0,5); //this works
    cout << numVuelo[n] <<endl;
}

void Vuelo::setDestino(string _destino, int n)
{
    destino[n] = _destino.substr(7, 13);                              //PROBLEM HERE
    cout << destino[n] << " " << destino[n].length() << endl;
}

void Vuelo::setPrecio(string _precio, int n)
{
    string p = _precio.substr(15,19);                                 //PROBLEM HERE
    precio[n] = atof(p.c_str());
    cout << precio[n] <<endl;
}

And finally my main looks like this:

#include "Vuelo.h"
#include <iostream>
#include <fstream>
#include <string>
#define NUM_FLIGHTS 10

using namespace std;

int main()
{
    cout << "Bienvenido, reserva tu vuelo!" << endl;
    cout << "-----------------------------------" << endl;

    Vuelo* flight = new Vuelo(NUM_FLIGHTS);

    flight->setM(); 

    return 0;
}

Thanks :)

Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
Pablo R.R.
  • 13
  • 6
  • Are you sure that the file "flights.txt" contains at least `NUM_FLIGHTS` lines? Why don't you count it in the string loop and don't use a vector of strings? Also `(!F.eof())` is a bad practice: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – 273K Dec 01 '19 at 16:17
  • It does, flights.txt contains 10 flights, just didn't put them all in there. – Pablo R.R. Dec 01 '19 at 16:18
  • I suggest to redesign your code in the real C++ way and you will forget about *somehow "destino" doesn't work without + 1*. There is no sense in fixing of the existing not reliable code. – 273K Dec 01 '19 at 16:23
  • Your Vuelo::setM() method is using a 1 based index rather than a zero based index when reading in the flight information. You initialize c to 1. – John Sheridan Dec 01 '19 at 16:26
  • 1
    Huh!! Thanks!! also, should I use **>> data** instead of !F.eof()? Im new to this – Pablo R.R. Dec 01 '19 at 16:28
  • 1
    John that was it!! – Pablo R.R. Dec 01 '19 at 16:31

0 Answers0