-4

Hello i got this code which is working fine but i fail to understand some parts of it and i could really use some help if anyone has some time to spare. I dont understand why is there a pointer *hightway on vector and why is there a star in vector? None of the vectors i found use stars. Even the slightest of help is welcome, thanks in advance.

int main() {


    int menuSelection,entryPoint,exitPoint,vehicleType;
    string plate,entryTime,exitTime;
    Vehicle * v = NULL;
    vector<Vehicle *> * highway;
    highway = new vector<Vehicle*>;




    do {
        mainMenu:
        cout << "\n\n\tKENTPIKO MENOY\n";
        cout << "\t[1] Eisodos oximatos\n";
        cout << "\t[2] Eksodos oximatos\n";
        cout << "\t[0] Eksodos apo to programma\n\n";
        cout << "\tEpilogi: ";
        cin >> menuSelection;

        switch(menuSelection) {
        case 0:

            cout << "\n\nEuxaristoume pou xrisimopoihsate to programma mas!";
            return 0;
        case 1:

            cout << "\n\n\tEISODOS OXIMATOS";

            do {
                cout << "\n\tSe poio simeio briskeste ; (1 ews 3) \t";
                cin >> entryPoint;
            } while(entryPoint > 3 || entryPoint == 0);

            cout << "\n\tArithmos kukloforias: ";
            cin >> plate;

            cout << "\n\tTupos oximatos\t";
            cout << "\n\t1: Dikikla\t2:Autokinita\t3:Fortiga\n";
            do {
                cout << "\tEpilogi: ";
                cin >> vehicleType;
            }while (vehicleType > 3 || vehicleType == 0);
            cout << endl;
            if (vehicleType==1){
                v = new Motorcycle();
            }
            else if (vehicleType==2){
                v = new Car();
            }
            else if (vehicleType==3){
                v = new Truck();
            }
            v->setPlate(plate);
            v->setEntryPoint(entryPoint);
            v->setEntryTime(getTime());
            highway->push_back(v);
            cout << "\n\n\tTo autokinito mpike stis " << v->getEntryTime() << " apo to simeio " << v->getEntryPoint();
            cout << "\n\n\tParakalo paralabate to eisitirio";
            v = 0;
            goto mainMenu;
        case 2:

            cout << "\n\n\tEKSODOS OXIMATOS";

            cout << "\n\tArithmos kukloforias: ";
            cin >> plate;
            for(vector<Vehicle*>::iterator it = highway->begin(); it != highway->end(); it++){
                if (plate.compare((*it)->getPlate()) == 0){
                    do {
                        cout << "\n\tSe poio shmeio briskeste; (" << (*it)->getEntryPoint()+1 <<" ews 4) \t";
                        cin >> exitPoint;
                    }while(exitPoint > 4 || exitPoint <= (*it)->getEntryPoint());
                    (*it)->setExitPoint(exitPoint);
                    (*it)->setExitTime(getTime());


                    cout << "\tArithmos kukloforias: " << (*it)->getPlate() << endl << endl;
                    cout << "\tSimeio eisodou: " << (*it)->getEntryPoint() << endl;
                    cout << "\tOra eisodou : " << (*it)->getEntryTime() << endl;
                    cout << "\tSimeio eksodou : " << (*it)->getExitPoint() << endl;
                    cout << "\tOra eksodou  : " << (*it)->getExitTime() << endl << endl;
                    cout << "\tSinoliki apostasi: " << (*it)->totalDistance() << "km"<< endl;
                    double km;
                    km=(*it)->totalFee()/(*it)->totalDistance();
                    cout << "\n\tkostos ana klm: " << km;
                    cout << "\n\n\tSINOLIKO POSO: " << (*it)->totalFee();
                    cout << "\n\n\tH apodeiksi einai ston ektupoti";
                }
                else {
                    cout << "\n\nTO OXIMA DEN YPARXEI!" << endl;
                    break;
                }
            }
            goto mainMenu;
        default:
            cout << "\n\n\tLATHOS EPILOGI! Dokimaste ksana.\n\n";
            goto mainMenu;
        }
    } while (menuSelection != 0);
    return 0;
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
Marinos
  • 5
  • 4
  • 4
    `highway` being a pointer is completely useless here, and it isn't even cleaned up. – chris Jan 19 '16 at 22:41
  • if i remove anything the program does not compile so why is it useless? – Marinos Jan 19 '16 at 22:42
  • If you change it to not be a pointer, you have to change all the uses to match, or of course it won't compile. It's bad because it's initialized on the very next line and not modified after that, plus it's never null. All the pointer is doing is introducing ugly syntax over a plain vector object and requiring ugly manual code to free the memory, which the code sample seems to have forgotten. If you truly need it to be allocated dynamically, you can solve the problem of manual cleanup with a smart pointer, but dynamic allocation is evidently not useful in this case due to the first points. – chris Jan 19 '16 at 22:47
  • Are you asking *what these stars mean*, or *why the code was designed this way?* – Beta Jan 19 '16 at 22:47
  • 1
    That's really horrible code and a particularly bad example to learn C++ programming from. You might want to pick a [decent book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start studying it, rather than working with poor quality examples like this. – Paul R Jan 19 '16 at 22:47
  • Duplicate (same user): http://stackoverflow.com/questions/34838819/how-to-fill-a-vector-with-objects-using-pointers (what was wrong with the answers that you got when you asked this same question two days ago ?). – Paul R Jan 19 '16 at 22:50
  • i am asking both what they mean and why are they used, also this code is made by a student so dont be so harsh – Marinos Jan 19 '16 at 22:50
  • The pointers inside the vector are at least useful for polymorphism, which you can learn from any introductory book. However, the manual cleanup point still applies, meaning smart pointers are helpful once again. – chris Jan 19 '16 at 22:50
  • `goto mainMenu;` Absolutely unnecessary. Not only is this frowned upon, it defeats the purpose of having the `do-while` loop in the first place. – PaulMcKenzie Jan 19 '16 at 22:50
  • @Marinos How is the code being written by a student forbid us by being harsh? It's like students are allowed to know nothing about the courses that they learned (or should've learned). – Algirdas Preidžius Jan 19 '16 at 22:53

1 Answers1

4

I dont understand why is there a pointer *hightway on vector

because its a pointer to vector<Vehicle *>, in line highway = new vector<Vehicle*>; you see that it is initialized with pointer to memory allocated with new.

and why is there a star in vector?

you mean in Vehicle* ? it because this vector contains pointers to Vehicle objects. Each element is a pointer to some dynamically allocated Vehicle object.

You dont really need highway to be a pointer, you may use:

vector<Vehicle *>  highway;

and later on use highway.push_back instead of highway->push_back.

marcinj
  • 48,511
  • 9
  • 79
  • 100