reading unnecessary empty space (urgent)
This code is supposed to read this input
graham crackers; 2 squares 59
milk chocolate; 1 bar 235
cheese, swiss; 1 oz 108
marshmallows; 1 cup 159
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
vector<string> nutrients_food;
vector<int> nutrients_amount;
vector<string> nutrients_servings;
vector<int> nutrients_calories;
ifstream inFile;
inFile.open ("nutrients0.dat");
if(!inFile)
cout << "No file found";
while(!inFile.eof())
{
string nut;
int amount;
string servings;
int calories;
while(getline(inFile,nut,';') && inFile >> amount >> servings >> calories)
{
nutrients_food.push_back(nut);
nutrients_amount.push_back(amount);
nutrients_servings.push_back(servings);
nutrients_calories.push_back(calories);
}
}
inFile.close();
|
it reads it fine but calling nutrients_food[i] results in
graham crackers
milk chocolate
cheese, swiss
marshmallows
|
How can I make the vector so it would ignore or delete the empty lines?
Last edited on
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <cctype>
while(getline(inFile,nut,';') && inFile >> amount >> servings >> calories)
{
nutrients_food.push_back(nut);
nutrients_amount.push_back(amount);
nutrients_servings.push_back(servings);
nutrients_calories.push_back(calories);
while ( inFile && isspace(inFile.peek()) )
inFile.ignore() ;
}
|
You sir or mam,
are a life saver
Topic archived. No new replies allowed.