Calculation inventory total from total values in a while loop

Oct 3, 2012 at 8:47pm
This is my assignment and I'm a bit stuck. Just need someone to point me in the right direction please. In this assignment, we're supposed to read data from notepad consisting of Part Name, Number of Units, Price and Total Value using a while loop. Then, we calculate the total value of parts by multiplying number of units by the price. I've done this correctly, but the second parts asks for the Inventory total, which is the addition of all the total values in the loop.

here is my code(sorry I don't know how to post it in the correct format yet):

//variable declarations
string pName = string ();
int numUnits = 0;
double price = 0.0;
double tValue = 0.0;
double iTotal = 0.0;
char dot = 149;
ifstream inData;
ofstream outData;

//open file
inData.open ("Inventory.txt");
outData.open ("InventoryTable.txt");

//loop, calculation and output
while (!inData.eof())
{
//input
inData >> pName >> numUnits >> price;

//calculation
tValue = numUnits * price;
iTotal = tValue + tValue;

cout << fixed << setprecision(2);
outData << fixed << setprecision(2);

//output
cout << pName << "\t\t" << numUnits << "\t\t\t" << price << "\t\t\t" << tValue << "\t\t\t" << endl;
outData << pName << "\t\t" << numUnits << "\t\t\t" << price << "\t\t\t" << tValue << "\t\t\t" << endl;

cout << "Inventory Total ($)" << endl;
cout << iTotal << endl;

the calculation I made (iTotal = tValue + tValue), which is obviously incorrect, only adds the last value to itself. I omitted some lines, and can someone please tell me how to post codes in the proper format. Thank you

Oct 3, 2012 at 11:21pm
You want

iTotal += tValue;

This adds the original value plus the right value back into the left value.
Oct 4, 2012 at 12:12am
you're awesome, thanks so much. Now I just have to worry about aligning the columns.



do you know if there's a way for me to use char values inside setfill ()?
Topic archived. No new replies allowed.