1

the input of the function below is being stored in a list and it is volatile.can i store tht list or vector in a file so tht it will be permanent n delete or modify the things in it by opening the file itself. i have tried to add a file bt it didnt work .the file is storing the input bt nt the queue,so everytime i try to access the value inside the queue cannot

  void Flight::addFlight(){

        /* ----INITIALISE VARS----*/
        bool flag = false; // used in loops

        /* --Check for arrival, departure time validity (USED ONLY IN BOARDING & ARRIVING TIME)-- */
        vector<string> fields; // stores, splitted,  user input to be checked
        string temp; // stores user's input temp
        char* pch; // stores result of function strtok()
        int hour; // stores hour
        int min; // store minutes

        cout << "Add new flights by giving the following attributes: \n";

        // clean stream
        cin.clear();
        cin.ignore(256,'\n');

        /* --FLIGHT NUBMER-- */
        cout << "Flight Number: ";
        // get user's input
        getline(cin, temp);

        do{
            flag = true;

            // check input
            if (!checkNumber(temp)){
                cout << "Please insert a valid Flight Number! " << endl;
                flag = false;
                getline(cin, temp);
            }else if (Flight::flightExists( atoi(temp.c_str()) )) {
                cout << "This Flight already exists!" << endl;
                cout << "Please insert a valid Flight Number!" << endl;
                flag = false;
                getline(cin, temp);
            }else {
                flag = true;
                this -> flightNo = atoi(temp.c_str());
            }
        }while(!flag);

        /* --DEPARTURE-- */
        cout << "Departure: ";
        flag = false;

        // check input
        LOOP:do{
            getline(cin, temp);
            if ( (temp.length() <= 10) && (checkString(temp)) ){
                this -> from = temp;
                flag = true;
            }else {
                cout << "Please insert a valid Departure city! ";
                goto LOOP;
            }
        }while(!flag);

        /* --DESTINATION-- */
        cout << "Destination: ";
        flag = false;

        // check input
        LOOP2:do{
            getline(cin, temp);
            if ( (temp.length() <= 10) && (checkString(temp)) && (temp.compare(this -> from)) ){
                this -> to = temp;
                flag = true;
            }else{
                cout << "Please insert a valid Destination city! ";
                goto LOOP2;
            }
        }while(!flag);

        /* --DEPARTURE TIME-- */
        cout << "Boarding time (e.g. 19:40): "; //ask from user for the boarding time
        flag = false;

        // check input
        LOOP3:do{
            getline(cin, temp);

            if( temp.length() != 5 || !checkTime(temp) ){
                cout << "Please insert a valid boarding time (e.g. 19:40)! ";
                goto LOOP3;
            }

            char t_temp[temp.length()];

            strcpy(t_temp, temp.c_str());

            //split string
            pch = strtok(t_temp, ":");

            while(pch != NULL){
                fields.push_back(pch);
                pch = strtok(NULL, ":");
            }

            hour = atoi(fields[0].c_str());
            min = atoi(fields[1].c_str());

            // check time
            if ((hour >=0 && hour<=23) && (min>=0 && min <=59)){
                this -> t_leave.hour = hour;
                this -> t_leave.min = min;
                flag = true;
            }else{
                cout << "Please insert a valid boarding time (e.g. 19:40)! ";
                fields.clear();
            }

        }while(!flag);

        /* --ARRIVAL TIME-- */
        cout << "Arriving time (e.g. 21:40): ";
        flag = false;
        fields.clear(); // clear fields (because it was used before, at "DEPARTURE TIME")

        // check input
        LOOP4:do{
            getline(cin, temp);

            if( temp.length() > 5 || !checkTime(temp) ){
                cout << "Please insert a valid boarding time (e.g. 19:40)! ";
                goto LOOP4;
            }

            char t_temp[temp.length()];

            strcpy(t_temp, temp.c_str());

            //split string
            pch = strtok(t_temp, ":");

            while(pch != NULL){
                fields.push_back(pch);
                pch = strtok(NULL, ":");
            }

            hour = atoi(fields[0].c_str());
            min = atoi(fields[1].c_str());

            // check validity of time
            if ((hour >=0 && hour<=23) && (min>=0 && min <=59)){
                this -> t_arrive.hour = hour;
                this -> t_arrive.min = min;
                flag = true;
            }else{
                cout << "Please insert a valid arriving time (e.g. 19:40)! ";
                fields.clear();
            }

        }while(!flag);

        /* --TICKET COST-- */
        cout << "Ticket price: ";
        LOOP5:do{

            getline(cin, temp);
            flag = true;

            // check input
            if (!checkNumber(temp)){
                cout << "Please insert a valid ticket price!" << endl;
                flag = false;
                goto LOOP5;
            }else{
                flag = true;
                this -> cost = atoi(temp.c_str());
            }
        }while(!flag);

        /* --AIRCRAFT TYPE-- */
        cout << "Aeroplane type: ";
        getline(cin, this -> plane_type);
        while(this -> plane_type.empty()){
            cout << "Please insert a valid Aeroplane type!" << endl;
            getline(cin, this -> plane_type);
        }

        /* --No OF SEATS-- */
        cout << "Number of seats: ";
        LOOP6:do{

            getline(cin, temp);
            flag = true;

            // check input
            if (!checkNumber(temp)){
                cout << "Please insert a valid number of seats!" << endl;
                flag = false;
                goto LOOP6;
            }else{
                flag = true;
                this -> seats = atoi(temp.c_str());
            }
        }while(!flag);

        /* --No of BOOKED SEATS-- */
        cout << "Number of booked seats: ";
        LOOP7:do{

            getline(cin, temp);
            flag = true;

            // check input
            if (!checkNumber(temp)){
                cout << "Please insert a valid number of booked seats!" << endl;
                flag = false;
                goto LOOP7;
            }else if ( atoi(temp.c_str()) > this -> seats ) {
                cout << "Booked seats must be less than plane's seats!" << endl;
                flag = false;
                goto LOOP7;
            }else {
                flag = true;
                this -> booked_seats = atoi(temp.c_str());
            }
        }while(!flag);
        cout << endl;


        flist.push_back(*this); // add object to the flist

        Queue q(this -> flightNo); // create new queue for the newly added flight
        qlist.push_back(q); // add object to the qlist

        cout << "Flight No: "<< this -> flightNo << " was successfully added!" << endl;

    }

    void Flight::write_train(){
        fp.open("Flight.dat",ios::out|ios::app);
        f.addFlight();
        fp.write((char*)&f,sizeof(Flight));
        fp.close();
        cout<<"\n\nThe Trip Has Been Added ";
        getch();
    }

    void Flight::deleteFlight(int num){

     fp.open("Flight.dat",ios::in);
       while(fp.read((char*)&f,sizeof(Flight))){

            for (std::list<Queue>::iterator i = qlist.begin(); i != qlist.end(); ++i){
                if( num == i -> getNo() ){

                    // enter if waiting queue for the flight is NOT empty
                    if (!i -> isEmpty()) {
                        // delete object from flist
                        for (std::list<Flight>::iterator i2 = flist.begin(); i2 != flist.end(); ++i2){
                            if( num == (i2 -> flightNo) ){
                                i2 = flist.erase(i2);
                                i = qlist.erase(i);
                                cout << "Flight with number: " << num << " was successfully deleted" << endl;
                                return;
                            }
                        }
                    }else{
                        cout << "There are passengers in the queue of the flight with No: " << num << endl;
                        cout << "Remove ALL of them from the queue first!" << endl;
                        return;
                    }
                }
            }
    }

            fp.close();
            cout << "This flight number doesn't exist!" << endl;
            return;
    }
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
gago
  • 11
  • 1
  • That's too much code. Wouldn't it be better to make a [mcve] focused onto your queue/vector/file issue? [Divide and conquer](https://en.wikipedia.org/wiki/Divide-and-conquer_algorithm) may help to solve your issue by yourself or having a code sample which will be accepted better here. (Btw. in production, I use the same trick - try out something in small until I (believe to) know how it works and then apply it to the product.) – Scheff's Cat Sep 28 '19 at 09:17
  • O.T.: `goto LOOP;` that's bad style. A simple [`continue;`](https://en.cppreference.com/w/cpp/language/continue) or even nothing would have done as well. (For what did you introduce the test `while (!flag)`?) – Scheff's Cat Sep 28 '19 at 09:19
  • thank you for the reply. actually what my question is that if we push some variables which is obtained from the user inside a queue,is there any possible way for us to store those data permanently so that we can access it back even when we close and reopen the program.thank you. – gago Sep 28 '19 at 11:11
  • The usual way is: store the data to a file when/before program exits, load the data from a file just after program start. A DB may be an alternative approach but more effort and you have to question whether it's worth. On embedded systems, writing to EPROM or persistent RAM may be an alternative as well but this is system specific. – Scheff's Cat Sep 28 '19 at 11:14

0 Answers0