-1

I am learning c++ and I can't wrap my head around this problem. I have created a class object which hold information of a vehicle. And the class name is vehicle. Here is the class:

class vehicle {
    private:
        string vehicleNumber;
        int vehicleType;

    public:

        //Default Constructor
        vehicle();

        //Overload Constructor - @params<vehicleNumber, vehicleType>
        vehicle(string vehicleNo, int type){
            vehicleNumber = vehicleNo;
            vehicleType = type;
        }

        //Some stuff to test
        string getNo(){
            return vehicleNumber;
        }

        int getType(){
            return vehicleType;
        }   

    };

And now in my main I have a void method which then takes in user input and puts the object in a vector. So in my main:

//Ref
void storeVehicle(vector<vehicle>&);

void storeVehicle(vector<vehicle>& createNewVehicle){

    string number;
    int type;

    cout << "Enter Vehicle Number: ";
    cin >> number;
    cout << "Enter the vehicle type: ";
    cin >> type;

    vehicle newV(number, type);

    createNewVehicle.push_back(newV);

    cout << endl;

}

//

Now here is the problem I am facing. Inside of my main method, I am creating the object. And this works perfectly find except if I call the main method again, it initializes the object again and I loose my previous data. I am not sure how to tackle this.

Here is the main method:

int main(){

    vector<vehicle> newVehicle;

    storeVehicle(newVehicle);

    return main();

}

So here I am returning main() again so that it reruns. But when it does, i loose my previous vector which held the data. Now how can I keep the data and keep calling the storeVehicle method?

Edit I also have a switch case which I am using to determine what the user chooses as an option. It may be to display the vehicle information or it maybe to add a new vehicle. In this case too, how can I add the vehicle without loosing previous data. Here is my switch which is inside another method:

void mainMenu(){

    int option;

    cin >> option;

    switch(option){
        case 1: {

            vector<vehicle> newVehicle;

            storeVehicle(newVehicle);

            break;
        }
        default:
            cout << "Wrong option";
    }

}

So now in my main method, I am simply calling this switch method. Either way, I loose the data yes?

EDIT

I don't understand why people keep downvoting. Aren't we all here to learn? And yet I get a downvote. Sooner or later, stackoverflow decides to block me from asking questions.

Mohamed Ahmed
  • 195
  • 1
  • 3
  • 16
  • 2
    Why are you calling main again? Are you just wanting to continually add vehicles? If so, look at using a while loop to keep calling storeVehicle. – Sdyess Oct 02 '19 at 05:53
  • Yes sorry. That is what I want. I want to keep adding vehicles. In a later update, I will put these in a switch case so the user has the option to choose to enter data or do something else. For now this is what I cant figure out. Could you please elaborate. Thanks – Mohamed Ahmed Oct 02 '19 at 05:54
  • 1
    it's illegal to call `main` yourself: https://stackoverflow.com/questions/2532912/call-main-itself-in-c – bolov Oct 02 '19 at 05:56
  • 1
    Don't worry about the downvotes. Don't take them personally. They are not towards you. Votes are on questions, not users. And they reflect the opinion of the community of the quality of the question and the usefulness of the question to other users. You will find questions with issues that should be learned from books, curricula, or tutorials are usually not well received here. I won't comment if that's a good or bad thing. And don't worry, stackoverflow won't ban you from asking questions unless you violate the site's policies, which is not the case here. – bolov Oct 02 '19 at 06:38

1 Answers1

2

Main isn't intended to be used like that, it's just an entry point for your application.

For what you're looking to do, you would be interested in looping. This way a certain section of your code can be ran repeatedly until a condition is met.

int main(){

    vector<vehicle> newVehicle;

    while(true)
    {
        storeVehicle(newVehicle);
    }

    return 0;

}

Take a look at how different loop types work. https://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm

Also, the one in my example is an infinite loop, since the condition is always true.

EDIT The question was changed.

The problem is that each time you call the mainMenu function that the vector is recreated. Create the vector in your Main function and then pass it by ref into the mainMenu function, so that it can then be passed into your storeVehicle function.

int main()
{
    vector<vehicle> newVehicle;

    while (true)
    {
        mainMenu(newVehicle);
    }

    return 0;
}
void mainMenu(vector<vehicle>& createNewVehicle) {

    int option;

    cin >> option;

    switch (option) {
    case 1: {
        storeVehicle(createNewVehicle);
        break;
    }
    default:
        cout << "Wrong option";
    }

}

You'd have to add a header for this function to work.

#include <limits>
void storeVehicle(vector<vehicle>& createNewVehicle) {

    string number;
    int type;

    cout << "Enter Vehicle Number: ";
    cin >> number;
    cout << "Enter the vehicle type: ";
    cin >> type;

    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    vehicle newV(number, type);

    createNewVehicle.push_back(newV);

    cout << endl;

}
Sdyess
  • 340
  • 1
  • 8
  • 1
    Place your vector outside of the switch statement. @MohamedAhmed Also I provided an edited answer as to how you can maintain your vector's state with the switch. – Sdyess Oct 02 '19 at 06:06
  • Oh no. I feel really stupid. Understood the problem and fixed. But I did it the nasty way. What I did was took the vector out of switch and put the switch in while(true). If I were to create a ref, how would have I done it? – Mohamed Ahmed Oct 02 '19 at 06:12
  • 1
    @MohamedAhmed I've added a code example. The cin.clear() and numeric_limits line are used to clear out the buffer between each vehicle's data entry. You can read more about it here https://gist.github.com/jpkrause/4a1aa400d45197ca3253 – Sdyess Oct 02 '19 at 06:28
  • Thank you so much! This helped a lot. I will clear the buffer after each entry. Also, I have updated my code before you updated your answer. But thank you anyways – Mohamed Ahmed Oct 02 '19 at 06:32