0

I'm writing a genetic algorithm using C++ in Eclipse 2019-09 to find an optimum aircraft landing schedule. I've been writing the program in pieces, starting at the beginning and adding them to the code, then testing them. After declaring some variables and constants,my program opens a file containing my data, reads it into an array of object type Airplane, then opens a second file for separation data, then makes some initial solutions. All of that was working.

Then I added code to fill up mu initial generation. Now my program stops after opening the first data file (successfully). It won't even read in the data. I haven't changed that part of the code; all of my changes are later in the program. I'm including my main program to the end of the block where it stops. It stops running after it prints "Reading from the file".

I'll also include the only class that is called before that point.

I'm sorry that all of my code won't go into the code block properly.

Does anyone have any ideas on what might be causing this?

#include <iostream>
#include <fstream>
#include <string>
#include <bits/stdc++.h>
#include "Airplane.h"
#include "LandingTime.h"
#include "LL.h"

//#include "Parameters.h"

using namespace std;


// Global Parameters and Constants -- change values here to fit your problem
const int P = 20; // number of planes
const int generationSize = 12;
const int maximumNumberOfGenerations = 5;
const int g = 2; // rate of cost per unit time for early landings
const int h = 3; // rate of cost per unit time for late landings
int seed = 2019;

const string filename = "CodeTestingData20planes.txt";




// Function Declarations
// void myFunction(int param[10])
int FindTotalCost(LandingTime schedule[P]);

int FindCost (int x, int t);

bool IsNotFeasible(LandingTime Solution[P], int sep[4][4]);
// The IsNotFeasible function also fixes some feasibility issues related to separation times


void Sort(LandingTime Solution[P]);

int main()
{
//Collect Data



// Collect array of airplanes
    Airplane airplane[P];
    int ac;
    int ty;
    int a;
    int e;
    int t;
    int l;
ifstream datafile;
datafile.open(filename);
if(!datafile.is_open()){
    cout << "file didn't open";
} else
{
    cout << "Reading from the file" << endl;
    for(int p = 0; p < P; p++)
            {
                  cout << p;
                  datafile >> ac;
                  datafile >> a;
                  datafile >> ty;
                  datafile >> e;
                  datafile >> t;
                  datafile >> l;

                  airplane[p] = Airplane(ac, a, ty, e, t, l);


                   // write the data at the screen.
                   cout << airplane[p].getAircraft() << "\t" << airplane[p].getType() << "\t";
                   cout << airplane[p].getA() << "\t" <<airplane[p].getE() << "\t";
                   cout << airplane[p].getT() << "\t" << airplane[p].getL();
                   cout << "\n";
    }//end of for p loop

    datafile.close();
    cout << "data file closed";

} // end of else

This is the class cpp file:

#include "Airplane.h"

namespace std {

Airplane::Airplane() {
    // TODO Auto-generated constructor stub

}

Airplane::Airplane(int ac, int a, int ty, int e, int t, int l){
    aircraft = ac;
    A = a;
    type = ty;
    E = e;
    T = t;
    L = l;
}

Airplane::~Airplane() {
    // TODO Auto-generated destructor stub
}


void Airplane::setAircraft(int ac){
    aircraft = ac;

}
void Airplane::setType(int ty){
    type = ty;
}
void Airplane::setA(int a){
    A = a;
}
void Airplane::setE(int e){
    E = e;
}
void Airplane::setT(int t){
    T = t;
}
void Airplane::setL(int l){
    L = l;
}

int Airplane::getAircraft(){
    return aircraft;
}
int Airplane::getType(){
    return type;
}
int Airplane::getA(){
    return A;
}
int Airplane::getE(){
    return E;
}
int Airplane::getT(){
    return T;
}
int Airplane::getL(){
    return L;
}



} /* namespace std */

This is the class .h file

#ifndef AIRPLANE_H_
#define AIRPLANE_H_

namespace std {

class Airplane {
public:

    // Class member variables
    int aircraft;   // Airplane identifier
    int A;          // Arrival time in ATC airspace
    int type;       // Type of airplane
    int E;          // Earliest possible landing time
    int T;          // Target landing time based on cruise speed
    int L;          // Latest possible landing time


    Airplane();
    Airplane(int ac, int a, int ty, int e, int t, int l);
    virtual ~Airplane();

    void setAircraft(int ac);
    void setA(int a);
    void setType(int ty);
    void setE(int e);
    void setT(int t);
    void setL(int l);

    int getAircraft();
    int getA();
    int getType();
    int getE();
    int getT();
    int getL();

};

} /* namespace std */

#endif /* AIRPLANE_H_ */

Mind you, this was all working and nothing was changed and now it's not working.

howlger
  • 22,720
  • 8
  • 35
  • 64
Catherine
  • 67
  • 5
  • 2
    Please use a debugger, which will tell you exactly at which line the program terminates and why. – walnut Dec 15 '19 at 18:10
  • I'm having trouble with Eclipse, being new to it. I cannot find the lines numbers in the debugger, but the program prints out the phrase "Reading from file", but it won't print p, which tells me that its either not entering the for loop, or getting stuck right after it enters. – Catherine Dec 15 '19 at 18:22
  • That is a wrong conclusion. `cout < – walnut Dec 15 '19 at 18:25
  • Probably unrelated but still recommended reading: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 Dec 15 '19 at 18:25
  • [How can I show line numbers in Eclipse?](https://stackoverflow.com/questions/640884/how-can-i-show-line-numbers-in-eclipse) – user4581301 Dec 15 '19 at 18:28
  • [Eclipse documentation on the CDT Debugger](https://help.eclipse.org/2019-09/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_o_debug.htm) – user4581301 Dec 15 '19 at 18:31
  • 1
    You should be validating the stream state after ***every*** input, e.g. `if (!(datafile >> ac)) { std::cerr << "error: invalid integer input - ac.\n"; exit(EXIT_FAILURE); }` A *"was working"* and now *"not working"*, (absent a new coding error), generally points to an invalid memory use error in the previous code that is now triggered by what was added. – David C. Rankin Dec 16 '19 at 00:46
  • @DavidC.Rankin But the OP is saying their program is freezing rather than crashing - which hints at a blocking IO operation or a thread blocked by a synchronization primitive. – Dai Dec 16 '19 at 01:08
  • @Dai Read that way I would agree completely. – David C. Rankin Dec 16 '19 at 01:09

0 Answers0