I have written a code that asks the user a name (ex.candy) and a number then it stores it in a stuff.txt file like so:
"
candy
20
candy
40
decorations
70
costumes
40
candy
60
"
When reading from that stuff.txt file, how can I add the running total for let say candy?
I want the program to recognize the string "candy" then adds the running total of the numbers below it then display it in a cout.
The part of the code that I am having trouble with:
ifstream file;
file.open("stuff.txt", ios::out);
cout << "You chose total sales for today.\n";
if (file.fail())
{
cout << "Error opening input file\n\n";
}
else
{
string candy, costumes, decorations;
if (file)
{
getline(file, candy);
file >> totalCandy;
cout << candy << "\n" << totalCandy;
file.get();
file.get();
getline(file, costumes);
file >> totalCostumes;
cout << costumes << "\n" << totalCostumes;
getline(file, decorations);
file >> totalDecorations;
cout << decorations << "\n" << totalDecorations;
file.close();
file.clear();
}
}
Thanks in advance!! (: