1

I build a win32 console application program, here is the source code:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct CPassenger
{
    string name;
    string ID;
    string seat;
};

struct CFlight
{
    string flNum;
    string destination;
    int amount;
    int booking;
    string departureTime;
    string fallTime;

    vector<CPassenger> list;
};

class CFlightSystem
{
public:
    CFlightSystem();
    ~CFlightSystem();
private:
    vector<CFlight> flight;
};

CFlightSystem::CFlightSystem()
{
    ifstream infile("flight.txt");

    if(!infile)
    {
        cerr<<"No input file!"<<endl;
        exit(1);
    }
    while(!infile.eof())
    {
        CFlight plane;
        infile>>plane.flNum>>plane.destination
              >>plane.amount>>plane.booking
              >>plane.departureTime>>plane.fallTime;
        for(int i=0;i!=plane.booking;++i)
        {
            CPassenger tmp;
            infile>>tmp.name>>tmp.ID>>tmp.seat;
            plane.list.push_back(tmp);
        }
        flight.push_back(plane);
    }

    infile.close();
}

CFlightSystem::~CFlightSystem()
{
    ofstream outfile("flight.txt");

    if(!outfile)
    {
        cerr<<"No output file!"<<endl;
        exit(1);
    }
    for(vector<CFlight>::iterator iter=flight.begin();
                                  iter!=flight.end();++iter)
    {
        outfile<<iter->flNum<<' '<<iter->destination<<' '
               <<iter->amount<<' '<<iter->booking<<' '
               <<iter->departureTime<<' '<<iter->fallTime<<' '
               <<endl;

        for(vector<CPassenger>::iterator it=(iter->list).begin();
                                         it!=(iter->list).end();++it)
        {
            outfile<<it->name<<' '
                   <<it->ID<<' '
                   <<it->seat<<endl;
        }
    }
    outfile.close();
}

int main()
{
    CFlightSystem management;
    return 0;
}

when I debug the code , I found that the console didn't return any messege that is to say, the main function is still called ? and I don't know if my destructor is working as I hope..

I'm a c++ freshman, and it's my first time posting here... (sorry for my poor English..I hope I can get some help ..T.T)

Will
  • 43
  • 6
  • 1
    My advice would be to either step through the program in a debugger, or insert print statements at various points to figure out how far it gets before it stops responding. – NPE Aug 28 '13 at 16:45
  • 1
    Also, "fall time" does not strike me as a particularly good choice of variable name (unless this for an NTSB system ;-)) – NPE Aug 28 '13 at 16:47
  • 3
    How do you expect the console to show anything when you are not printing anything under the normal circumstances? You may want to add `cerr << "Done!" << endl` before the final `return 0`, or add "tracing" output to the constructor and the destructor to see what path through the code your program took. – Sergey Kalinichenko Aug 28 '13 at 16:48
  • 1
    do you mean program hangs out or it just doesn't print to the console but program gets executed? – 4pie0 Aug 28 '13 at 16:48
  • 4
    `while(!infile.eof())` is wrong: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – gx_ Aug 28 '13 at 16:52
  • @computer I found that if I create a blank file "flight.txt", and debug it . then the console window wouldn't close – Will Aug 29 '13 at 01:27
  • yes, this is probably because of while(!infile.eof()), change it into while(infile>>plane.flNum) and adjust code accordingly – 4pie0 Aug 29 '13 at 01:36
  • Just now I adjust my code : `while(true){ if(!infile.eof()) break; /*do some*/ }` and it solved the promblem. although I don't know why... @computer – Will Aug 29 '13 at 02:19

0 Answers0