File txt reader
Oct 11, 2013 at 3:11am UTC
I have this code I wrote today in class. I am nearly complete but I need to to add the total cost for each item but I don't know how to come up with an equation to add them since I only have one cout statement and all the values are being read at the same time.
This is what is what is being read in the txt file.
Chisel 50 9.99
Hammer 30 15.99
Nails 2000 0.99
Bolts 200 2.99
Nuts 300 1.99
Soap 55 1.89
And here is my code
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string item;
int amount;
double price;
double cost;
double invTotal;
ifstream in;
in.open("input.txt" );
ofstream out;
out.open("output.txt" );
cout << fixed << setprecision(2);
for (int j=0; j < 39; j++)
{
cout << " " << (char )220;
}
cout << "\n" ;
cout << "\t\tInventory Report for Jane Doe International Hardware\n" ;
for (int j=0; j < 39; j++)
{
cout << " " << (char )220;
}
cout << "\n" ;
cout << "Item" ;
cout << "\tNumber of Units" ;
cout << "\t\tUnit Cost($)" ;
cout << "\t\tTotal Value($)\n" ;
for (int j=0; j < 79; j++)
{
cout << (char )196;
}
cout << "\n" ;
while (!in.eof())
{ in >> item >> amount >> price;
cost = amount * price;
cout << left << setw(10) << item
<< right << setw(5) << amount
<< right << setw(25) << price
<< right << setw(30) << cost
<< "\n" ;
}
for (int j=0; j < 79; j++)
{
cout << (char )196;
}
cout << "\n" ;
cout << "Inventory Total($):" ;
return 0;
}
Oct 11, 2013 at 3:35am UTC
Create a variable to store all the cost.
Example:...err....the answer
1 2 3 4 5 6 7 8 9 10
double total = 0;
//And then in the while - loop where you read in stuff, and calculate cost.
//Right after you calculate cost, try this:
total = total + cost;// Or total += cost;.......they mean the same thing.
Last edited on Oct 11, 2013 at 3:36am UTC
Oct 11, 2013 at 4:11am UTC
thanks dude! It really helped and I was able to make a sense of why you set total equal to zero. :)
Topic archived. No new replies allowed.