Array help

I need to add up all the pounds of food sold from this inFile. I cannot get the array right?

Joe Ross 50 F
George Goffer 32 P
Force Stop 41 B
Vever Bord 88 Y
Corn Morn 12 W

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
    string firstName;
    string lastName;
    int pounds=0;
    char food;
    int count = 0;
    double costlb=0.00;
    double totalcost;
    int totalpounds=0;
  
    
    
    
    cout << "Name " << "\t\tQuantity(lbs)" << "\tDogfood Type" << "\tCost per lb  " << "\tTotal Cost\n" << endl;
    
    ifstream inFile;
    inFile.open("V2_new.txt",ios::in);
    if(!inFile)
    {
               cout << "\nFile Error\n";
               system("PAUSE");
               return 1;
               }
    
    //------------------------------
    inFile >> firstName >> lastName >> pounds >> food;
    
    while(inFile)
    {
    count++;
    
    if (inFile)
    {  pounds++;
}
    
    if(food == 'F')
    {       
            costlb = 11.00;
}
    
    if(food == 'P')
    {
    costlb = 21.50;
}
    
    if(food == 'B')
    {
            costlb = 22.50;
            }
    
    if (food == 'Y')
    {
    costlb = 14.00;
}
    
    if (food == 'W')
    {
             costlb = 5.50;
             }
        
      
     if (pounds >= 15)
    {           
               totalcost = costlb * pounds;
               }
    if (pounds < 15)
    {        
               totalcost = (costlb * 0.15) + (costlb * pounds);
               }
   
    
    
    cout << "\n" << firstName << " " << lastName << "\t\t" << pounds << "\t" << food << "\t\t$" << costlb << "\t\t$" << totalcost << "\n";
    
    
   
    
    inFile >> firstName >> lastName >> pounds >> food;
}


             
 cout << "\nTotal number of customers: " << count;
 cout << "\nTotal pounds sold: " << pounds;
 
 
    
    
    
    inFile.close();
    cout <<"\n\n"; 
    system ("PAUSE");
    return EXIT_SUCCESS;
}
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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string FirstName, LastName;
    int Pounds = 0, TotalPounds = 0, Count = 0;
    char Food;
    float CostLB = 0.00, TotalCost = 0.00;

    ifstream inFile;
    inFile.open("inventory.txt", ios::in);
    if (!inFile)
    {
        cout << "\nFile Error\n";
        system("pause");
        return 1;
    }
    inFile >> FirstName >> LastName >> Pounds >> Food;
    while (inFile)
    {
        Count++;
        TotalPounds += Pounds;
        if (Food == 'F')
        {
            CostLB = 11.00;
        }
        if (Food == 'P')
        {
            CostLB = 21.50;
        }
        if (Food == 'B')
        {
            CostLB = 22.50;
        }
        if (Food == 'Y')
        {
            CostLB = 14.00;
        }
        if (Food == 'W')
        {
            CostLB = 5.50;
        }
        if (Pounds >= 15)
        {
            TotalCost = CostLB * Pounds;
        }
        if (Pounds < 15)
        {
            TotalCost = (CostLB * 0.15) + (CostLB * Pounds);
        }
        cout << "Name : " << FirstName << " " << LastName << endl;
        cout << "Quality(Pounds): " << Pounds << endl;
        cout << "Dog Food Type: " << Food << endl;
        cout << "Cost Per Pound: $" << CostLB << endl;
        cout << "Total Cost: $" << TotalCost << endl;
        cout << endl;
        inFile >> FirstName >> LastName >> Pounds >> Food;
    }
    cout << "\nTotal Number of Customers: " << Count;
    cout << "\nTotal Pounds Sold: " << TotalPounds;
    inFile.close();
    cout << "\n" << endl;
    system("pause");
    return EXIT_SUCCESS;
}


I changed your variable names slightly and your format. It was too messy. Your only fault was the following code:

Pounds++; //This was your code.

TotalPounds += Pounds; //This is the correct code.

Basically, the only problem was you didn't use your "TotalPounds" variable.
Instead, you only incremented your "Pound" variable by one each time.

If you fix these problems your array should work properly.
Last edited on
Topic archived. No new replies allowed.