Hi had a question about reading a file with a int and a string and putting it into a vector/array
The file is called options.txt and contains:
5000 Leather Seats
1000 DVD System
800 10 Speakers
1400 Navigation System
500 CarPlay
500 Android Auto
2000 Lane Monitoring
800 3/36 Warranty
999 6/72 Warranty
1500 Dual Climate
225 Body Side Molding
49 Cargo Net
87 Cargo Organizer
700 450W Audio
1000 Heated Seats
I'm separating the prices and the options into their own vectors/arrays however I don't know how to process the whitespace between prices and options (also don't know how to include spaces for the options such as Cargo Net).
<< splits out whitespace for you.
so
file >> intvariable;
will read the integer.
but your input is complex with additional white space and terms for some data.
what do you want here, is it integer & one string ?
struct data
{
string s;
int i;
};
int main ()
{
vector<data> d;
data tmp;
ifstream ifs("dat");
while(ifs >> tmp.i)
{
getline(ifs,tmp.s);
d.push_back(tmp);
}
for(auto &a : d)
cout << a.i << " | " << a.s << endl;
}
5000 | Leather Seats
1000 | DVD System
800 | 10 Speakers
1400 | Navigation System
500 | CarPlay
500 | Android Auto
2000 | Lane Monitoring
800 | 3/36 Warranty
999 | 6/72 Warranty
1500 | Dual Climate
225 | Body Side Molding
49 | Cargo Net
87 | Cargo Organizer
700 | 450W Audio
1000 | Heated Seats
Note that mixing stream (>> and <<) with getline can be error prone. I believe I did it right here, but always double check that when you mix these tools. If you do not trust the file, you could add error checking ...