jeudi 24 mai 2018

How can I erase/delete Vector of Class Objects Values

P S : May be this question has already been asked but i tried a lot and also i'm not using pointer with vector. If the solution is that Please tell me how to use pointer here..

My Question Is:

I'm creating a Car Class with some objects and getter setter methods.and pushing new records inside it. Even i'm editing that records also but i dont know how to delete particular record??? I have putted the code in comments which i tried by myself so if anybody knows please telll me how can i remove/erase the particular record from vector class object?

thanks in advance...

My Car.cpp file

#include "Car.h"
#include "global.h"
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
int cid =1;

string Name;
float Price;

void printCarVector(vector<Car>&);

using namespace std;



Car::Car()
{
}


Car::Car(int id, string name, float Price, string SessUserName)
{
    newId = id;
    newName = name;
    newPrice = Price;
    newSessUserName=SessUserName;
}

Car::~Car()
{

}

int Car::getId() const
{
    return  newId;
}
string Car::getName() const
{
    return  newName;
}
float Car::getPrice() const
{
    return  newPrice;
}
string Car::getSessUserName() const
{
    return  newSessUserName;
}

void Car::setId(int id)
{
    newId = id;
}
void Car::setName(string name)
{
    newName = name;
}
void Car::setPrice(float Price )
{
    newPrice = Price;
}
void Car::setSessUserName(string SessUserName )
{
    newSessUserName = SessUserName;
}

void fillCarVector(vector<Car>& newAllCar)
{
    cout<<endl;
    cout << "Add Car Info" << '\n';

    cout << " Car Name : ";
    cin >> Name;
    cout << " Car Price : ";
    cin >> Price;

    Car newCar(cid,Name, Price,SessUserName);

    newAllCar.push_back(newCar);
    cid++;
    cout << endl;
}

//**In this function I want to delete the records**
void deleteCarVector( vector<Car>& newAllCar)
{
    int id;
    cout<<" \n\t\t Please Enter the Id of Car to Delete Car Details :  ";
    cin>>id;
 //replace(newAllCar.begin(),newAllCar.end(),"a","b");


    unsigned int size = newAllCar.size();
    for (unsigned int i = 0; i<size; i++)
    {

        if(newAllCar[i].getId()==id)
        {
            cout<<"Current Car Name : "<<newAllCar[i].getName()<<"\n";

       //Here Exactly the problem is..
           // delete newAllCar[i];

       //     newAllCar.erase(newAllCar[i].newName);
         //  newAllCar.erase(remove(newAllCar.begin(),newAllCar.end(),newAllCar.at(i).getId()));


        }
    }

    printCarVector(newAllCar);

    cout << endl;
}


void editCarVector( vector<Car>& newAllCar)
{
    int id;
    float NewCarPrice;
    string NewCarName;
    cout<<" \n\t\t Please Enter the Id of Car to Edit Details :  ";
    cin>>id;
 //replace(newAllCar.begin(),newAllCar.end(),"a","b");


    unsigned int size = newAllCar.size();
    for (unsigned int i = 0; i<size; i++)
    {

        if(newAllCar[i].getId()==id)
        {
            cout<<"Current Car Name : "<<newAllCar[i].getName()<<"\n";
            cout<<"Enter New Car Name : ";
            cin>>NewCarName;
               newAllCar[i].newName=NewCarName;

            cout<<"Current Car Price : "<<newAllCar[i].getPrice()<<"\n";
            cout<<"Enter New Car Price : ";
            cin>>NewCarPrice;
               newAllCar[i].newPrice=NewCarPrice;
        }
    }

    printCarVector(newAllCar);

    cout << endl;
}


void printCarVector( vector<Car>& newAllCar)
{

    cout<<"\n\n";
    cout<<"\t\t ----------------------------------------------\n"<< "\t\t Car Details \n";
    cout<<"\t\t ----------------------------------------------\n\n";

    cout<<setfill(' ')<<setw(10);

    cout<<setw(10)<<"Car Id"<<" | "<<setw(20)<<"Car Name"<<" | "<<setw(15) <<"Car price"<<" | "<<setw(15) <<"UserName" <<"\n";
    cout<<"--------------------------------------------------------------------------------\n";
    unsigned int size = newAllCar.size();
    for (unsigned int i = 0; i<size; i++)
    {
        cout << setw(10)<< newAllCar[i].getId()<<" | " ;
        cout << setw(20)<<newAllCar[i].getName()<<" | " ;
        cout << setw(15)<<newAllCar[i].getPrice()<<" | ";
        cout <<setw(15)<< newAllCar[i].getSessUserName()<<"\n";
    }
    cout<<"\n\n";
}

My Car.h Header file file

//Car Header

#ifndef CAR_H
#define CAR_H
#include <iostream>
#include <string>

using namespace std;

class Car
{

public:

    //default constructor
    Car();

    //overload Constructor
    Car(int, string, float,string);

    //destructor
    ~Car();

    //Accessor Fun
    int getId() const;

    string getName() const;

    float getPrice() const;
    string getSessUserName() const;
    //string getSessUserName() const;


    //mutator functions
    void setId(int);
    void setName(string);
    void setPrice(float);
    void setSessUserName(string);
  //Member Variables
    int newId;
     string newName;
    float  newPrice;
    string  newSessUserName;



};
#endif // !CAR_H

Aucun commentaire:

Enregistrer un commentaire