0

How do I store my comma separated file in a vector structure, not a CSV text file?

I have a file called FILE.txt which has data like this

Date,ID,ArrivalTime,DepatureTime
12/02/2022,BDF 021,12:30:20,_
13/02/2022,FSD 213,10:20:30,12:30:20

etc. I have a vector called vector<vehicle> vehic(100);. I want to store it in the vector such that it will be the date will be stored, ID etc

Function for adding vehicles

I add vehicles using a function

void vehicle::addVehicle()
{

      vehicle *v = new vehicle;
      cin.ignore();
      cout<<"Enter vehicle number : ";
      std::getline(cin, v->id);
      cout<<"Enter arrival time in hours minutes and seconds : ";
      cin>>v->arrive.hh>>v->arrive.col1>>v->arrive.mm>>v->arrive.col2>>v->arrive.ss;
      cout<<"Enter date in day month and year: ";
      cin>>v->dt.day>>v->dt.sym1>>v->dt.month>>v->dt.sym2>>v->dt.year;

      vehic.at(i).id=v->pltno;
      vehic.at(i).arrive.hh=v->arrive.hh;
      vehic.at(i).arrive.mm=v->arrive.mm;
      vehic.at(i).arrive.ss=v->arrive.ss;
      vehic.at(i).dt.day=v->dt.day;
      vehic.at(i).dt.month=v->dt.month;
      vehic.at(i).dt.year=v->dt.year;
}

I would like to store it in the same structure as I added them using this function. How would I do so?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
DragonKing
  • 1
  • 1
  • 5
  • 1
    When and where do you initialize `i`? When and where do you increment it? And why do you do it like that instead of the usual `push_back` (as in `veh.push_back(*v)`)? – Some programmer dude Apr 20 '22 at 07:21
  • 2
    Also, why is `v` a pointer? In C++ you don't need `new` to create objects. In fact, as you don't `delete` your object you will have a memory leak every time you call `addVehicle()`. – Some programmer dude Apr 20 '22 at 07:22
  • `int static totalvehicle=0,totalcar=0,totalamt=0,i=0; ` I Intilized I just before the function – DragonKing Apr 20 '22 at 07:27
  • Did you read the duplicate? If somthing about the duplicate doesn't answer what you are trying to understand, you need to state what that is. Or another route is to explain why the dup doesn't answer your question. In the current state, its hard to authorize the reopening of this question, it appears to have no different answer than the answer given to the other question, that your question is labeled as being a dup of. – JΛYDΞV Apr 21 '22 at 06:41

0 Answers0