Question

Problem:

I have a text file with lines of information in it as need below under "txt file". I am attempting to map the items so I can finish my assignment. In mapping them I am using istringstream. My problem comes about in getting it to work when there are multiple words in an item that I want to have saved in one string. For example the "Unsweetened Applesauce" I want that to be one string (item.setNewItem). Any help would really be appreciative, and since I am a current student, any dumbing down for my sake would really be appreciated. =)

txt file:

1 cup Sugar | 1 cup Unsweetened Applesauce | calorie

Code:

void mapList(ifstream &foodReplace, map<string, Substitutions> &subs)
{
    string line;
    while (getline (foodReplace, line));
    {
        Substitutions item;
        istringstream readLine(line);

        readLine << item.setOldAmount
                 << item.setOldMeasurement
                 << item.setOldItem
                 << item.setNewAmount
                 << item.setNewMeasurement
                 << item.setNewItem;

        subs.insert(pair<string, Substitutions>(item.getOldItem, item));
    }

}
Was it helpful?

Solution 2

I really appreciate everyone's help, it did help me get to where I needed even though I didn't use the exact same code, below is the solution I went with, again, thanks everyone. =)

//function to populate map and update object info
void mapList(fstream &foodReplace, map<string, Substitutions> &subs) 
{
    string line;
    while (getline (foodReplace, line)) //gets the line and saves it to line
    {
        Substitutions item;
        istringstream readLine(line); //reads it into readLine

        //declaring variables
        double amount;
        string unit;
        string ingredient;
        string benefit;

        //gets old ingredient and saves in object
        getIngredient(readLine, amount, unit, ingredient);
        item.setOldAmount(amount);
        item.setOldMeasurement(unit);
        item.setOldItem(ingredient);

        //gets new ingredient and saves to object
        getIngredient(readLine, amount, unit, ingredient);
        item.setNewAmount(amount);
        item.setNewMeasurement(unit);
        item.setNewItem(ingredient);

        //reads in last piece and saves in object
        readLine >> benefit;
        item.setBenefit(benefit);

        //inserts object into map
        subs.insert(pair<string, Substitutions>(item.getOldItem(), item));
    }
}

//function to extract amount-units-ingredient
void getIngredient(istringstream &stream, double &amount, string &unit, string &ingredient)
{
    //resetting variables
    amount = 0;
    unit = "";
    ingredient = "";
    string temp;

    //setting variables
    stream >> amount;
    stream >> unit;
    stream >> temp;

    //read until delimiter is hit
    while (temp != "|")
    {
        ingredient += temp + " ";
        stream >> temp;
    }

    //removes the space at the end of the ingredient
    ingredient = ingredient.substr(0, ingredient.length() - 1);
}

OTHER TIPS

You can just supply a third parameter to getline to specify the delimiter:

http://www.cplusplus.com/reference/string/string/getline/

And then you can read the first and second fields to ' ', and the third field to '|'.

The call would read:

void mapList(ifstream &foodReplace, map<string, Substitutions> &subs)
{
    string line;
    while (getline (foodReplace, line));
    {
        Substitutions item;
        istringstream readLine(line);

        getline(readLine, item.setOldAmount, ' '); //you may need to read this in to a temp string if setOldAmount is an int
        getline(readLine, item.setOldMeasurement, ' ');
        getline(readLine, item.setOldItem, '|');

        getline(readLine, item.setNewAmount, ' '); //you may need to read this in to a temp string if setNewAmount is an int
        getline(readLine, item.setNewMeasurement, ' ');
        getline(readLine, item.setNewItem, '|');

        subs.insert(pair<string, Substitutions>(item.getOldItem, item));
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top