I have a text file thats separated by colons for example
c:toyota:2006:corolla:56000 (type(car or truck):make:year:model:mileage)
I am running into errors on how to separate each and store them into respective variables
char type;
char *make;
char model; //Have to put model into an int*, so I have been typecasting into an int
char *model;
int mileage;
I am running into a problem where its reading the colons into the MAKE variable since its defaulted to char *make = new char[100]; since the make of the vehicles vary.
i have tried
ifstream fin;
fin >> type >> COLON >> make etc...
but just crashes the program as im assuming its just storing the whole into the make pointer.
You could use c-strings and strtok() to split the string at each colon.
Or use a stringstream. The following code assumes you have used getline() to read a single line from the file into a string.
So I am trying to store year and mileage into an integer pointer by doing a loop but realized that i am using a string variable so its not working correctly. Any tips ?
atoi() takes a character string and returns an integer - assuming the string contains a valid representation of an int, such as "-1" or "+23" or "7654". http://www.cplusplus.com/reference/cstdlib/
I'm having difficulty understanding what you need to do with model. I thought that was a string, such as "corolla". As such, it cannot be used with atoi() because it is ordinary text, not a numeric value.
In that context, the code you posted above doesn't seem to make sense, I wouldn't have any idea of how to fix it.
Perhaps you could describe what it is you need to do, or give the relevant part of the specifications for this project.