Adding values from a file

I'm having a bit of trouble trying to figure out how to get certain values to add up into a total value. This assignment was to create a program that would read values from a provided file. It would then sort them into columns and create an additional column called 'Value'. This would be the Quantity times the Cost values.

The last part would be to add up each value and display a total message. 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
 #include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    //Declare Variables
    string name = "";
    double quant = 0.0;
    double price = 0.0;
    double value = 0.0;
    double total = 0.0;
    
    //Create file object
    ifstream inFile;
    
    //Open File for Input
    inFile.open("Advanced25.txt", ios::in);
    if (inFile.is_open())
       {
          //Create columns
          cout << "Name" << '\t' << "Quantity" << '\t' << "Price" << '\t'
          << "Value" << endl;
          
          //Get item data
          getline(inFile, name, '#');
          inFile >> quant;
          inFile.ignore(1);
          inFile >> price;
          value = quant*price;
         
           
          while (!inFile.eof())
          {
                //Display Data
                std::cout << std::fixed;
                std::cout << std::setprecision(2);
                std::cout << name << '\t' << quant << '\t'                      
                << price << '\t' << value
                << endl;
                
                getline(inFile, name, '#');
                inFile >> quant;
                inFile.ignore(1);
                inFile >> price;
                value = quant*price;
                
                }//end while
                
                std::cout << "The total inventory value is: " << total << endl;
                
                //Close file
                inFile.close();
        }
        else
            cout << "File could not be opened." << endl;
        system("pause");
        return 0; 
}
          


I have tried a while and a for loop within the current while loop in order to add up the Values. However, that did not work. The total value ended up copying the first item's value when I used the additional while loop. With the For loop, every Value was altered instead of providing a new total.

What am I doing wrong? How should I go about finding the total of these items? I tried both While and For loops at line 50 but removed them since they didn't do anything productive.

Thanks for your time.
Your indentation is really confusing.

Move lines 38-43 to after line 49 and try it again -- see if you can figure out what is going wrong.

(Hint: don't loop on EOF.)


My brain is too far gone to do anything useful at the moment, so if the problem is anything beyond that post back.
Topic archived. No new replies allowed.