-4

I have a project for school. Need to read file and sort it by destinatiion, departure (24 hour format) , flightNo and gateNo. I tried to sort but it didn't work.. I am new in programming,and if anyone can help me, I'll appreciate.

// Name        : Flight.h

#pragma once
#include <iostream>
#include <string>
#include <vector>
using std::string;

class Flight
{
private:
    string flightNo;
    string destination;
    string departure;
    string gateNo;

public:
    Flight(void);
    ~Flight(void);
    void readingFile();
    void writingFile();

};

// Flight.cpp

#include "Flight.h"
#include <fstream>
#include <cstring>

using namespace std;

Flight::Flight(void)
{
}

Flight :: ~Flight(void)                 
{
}

void Flight :: readingFile()
{
    string FileName;
    string line;
    cout << "Enter a file name: " << endl;  
    cin >> FileName;
    ifstream inFile(FileName.c_str());
    if (inFile.is_open())
    {
        while (getline( inFile, line))
        {
            cout << line <<endl;
        }
        inFile.close();
    }else
        cout << "File is not open!" <<endl;
}
//Sort.h  
#pragma once

#include "Flight.h"
#include <vector>


// Sort class
class Sort
{
protected:
    // number of comparisons performed in sort function
    unsigned long num_cmps;
public:
    // main entry point
    //virtual void sort(vector<Flight>& data) = 0;     
    void sortflights();
    // returns number of comparisons
    unsigned long getNumCmps();
    // resets the number of comparisons
    void resetNumCmps();
};

// SelectionSort class
class SelectionSort : public Sort
{
public:
    // main entry point
    void sort(std::vector<Flight>& data);
};

class MergeSort : public Sort
{
    // main entry point
    void sort(std::vector<Flight>& data);
};


//Sort.cpp
#include "Sort.h"
#include <iostream>

using namespace std;

unsigned long Sort::getNumCmps()
{
    return num_cmps;
}


void Sort::resetNumCmps()
{
    num_cmps = 0;
}

void Sort :: sortflights()
{
    int choose;
    cout<<"Choose an option!"<<endl;
    cout<<"PRESS 1 FOR SORT BY DESTINATION"<<endl;
    cout<<"PRESS 2 FOR SORT BY DEPARTURE"<<endl;
    cout<<"PRESS 3 FOR SORT BY FLIGHT NUMBER"<<endl;
    cout<<"PRESS 3 FOR SORT BY GATE NUMBER"<<endl;
    cin>>choose;

    if(choose==1)
    {

        vector<string> destination;

        for(int i=0; i<destination.size(); i++)
        {
            string elem1=destination[i];
            string elem2=destination[i+1];

            if (strcmp(elem1.c_str(),elem2.c_str())>0)
            {
                cout << elem1<<endl ;
            }
            else
                cout << elem2<<endl ;
            cout << "Number of iteration = " << i << endl;
        }

    }
    else if(choose==2)
    {


        vector<string> departure;

        for(int i=0; i<departure.size(); i++)
        {}
newprog
  • 1
  • 2
  • You might want to read about [functors](http://stackoverflow.com/questions/356950/c-functors-and-their-uses) or [lambda expressions](http://en.cppreference.com/w/cpp/language/lambda), or just passing [function pointers](http://www.learncpp.com/cpp-tutorial/78-function-pointers/). That way you can pass a custom comparison function to your generic sorting function. This custom comparison function can use whatever member field of `Flight` it wants for comparison. – Some programmer dude Jan 21 '16 at 12:47
  • Can you use [`std::sort`](http://en.cppreference.com/w/cpp/algorithm/sort)? – NathanOliver Jan 21 '16 at 12:50
  • I can't use stl algorithm. – newprog Jan 21 '16 at 12:58
  • 1
    @newprog *I can't use stl algorithm* -- Then your question is too broad. There are multitudes of sorting algorithms. Which one? And if you can't use `std::sort`, why in blazes wouldn't you have been assigned something simple, like sort an array of `int`s? – PaulMcKenzie Jan 21 '16 at 13:11
  • That's my task. I don't know what to do any more. I tried everything. Can you please help me to do it,or explain how to do. I sad I am new in this. – newprog Jan 21 '16 at 13:19
  • _I tried everything_ Apparently not. :) – erip Jan 21 '16 at 13:22
  • Then go for plain old *bubble sort*. And there are plenty of information about different sorting algorithms all over the Internet if that's your problem. Just go to your favorite search engine and search a little. First get sorting for a single hardcoded field working, and then see my first comment for hints about a way to make it general for any field of the structure. – Some programmer dude Jan 21 '16 at 13:29
  • if i knew what to do i wouldn't ask. Apparently i don't know what to do.. – newprog Jan 21 '16 at 13:30
  • @JoachimPileborg I searched a lot but I couldn't fit that with my code. Ok, I will figure out. Thank you – newprog Jan 21 '16 at 13:43

1 Answers1

0

First off, there is no reason for flight number and gate number to be strings. they are just numbers. Translate the string from the file to an int. If you can't use stl, you'll just have to keep the datetime as a string I guess :(

Break this into pieces. You need three different comparisons: numeric, datetime, and lexicographical, depending on what you are sorting by.

A numeric sort is just number comparison: if( a < b ).

A lexicographical sort is relatively simple. Characters are just 8 bit numbers that map to a glyph stored somewhere. You can compare the strings character by character the same way you compare ints. Once you find characters that differ from each other, you can compare the chars and determine which is greater.

Datetime will be tricky without stl. You'll have to find a way to parse the string into something you can compare. Google ways to parse that string.

The only thing left is your sorting algorithm. Visit the Wikipedia pages on Insertion sort, Merge sort, Quicksort, and Selection sort. They give detailed descriptions on how each algorithm works, and in most cases pseudocode on their implementation.

There's really not much else to say on this subject.

James
  • 159
  • 2
  • 11
  • In your destination sort block, there is no list of destinations being brought in to be analyzed. The loop would quit before a single pass because the length is 0. In addition, you have zero bounds checking for when i+1 is out of bounds, which it always will be on the last pass. After that, you have to implement one of the sorts I mentioned are outlined on Wikipedia. – James Jan 21 '16 at 21:18
  • Ok. Thank you very much for explanation. – newprog Jan 21 '16 at 21:31