Arrays and calculations Please Help :'(

/* i have a problem with the calculation in case 2 (case 3 and 4 are the same). Its suppose to subtract the Weight in from the weight out and add all of them from the array. So for example Arima, the program is suppose to look for the indices that have r (recyclable garbage) and subtract the weight from the Weight out and then add them all together to give the total. the program runs but the calculations aren't correct. */

Contents of Data.txt


November24,Tunapuna-Piarco,r,3000.00,2500.00
November25,Armia,g,4000.00,3500.00
November04,San Juan-Laventille,r,4500.00,3500.00
November11,Chaguanus,r,4500.00,3000.00
November22,Tunapuna-Piarco,g,2000.00,1000.00
November25,Armia,r,5000.00,6000.00
November01,San Juan-Laventille,r,3000.00,3400.00
November11,Chaguanus,g,4700.00,3900.00







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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>


using namespace std;

int main()
{
    //declaring the variables
     char c;
    int i = 0;
    int Option = 0;
   string Date[8];
    string RegionalCop[8];
    char Typeog[8];
    double Weightin[8];
    double Weightout[8];
    double Arima =0.0;
    double ArimaToT = 0.0;
    double SanJuan = 0.0;
    double SanToT = 0.0;
    double Tuna = 0.0;
    double TunaTOT = 0.0;
    double Chaguanus = 0.0,ChaguanusTOT = 0.0;

do{

    //asking the user to enter and option

    cout<<"1. View the Logs."<<endl
    <<"2. View the total amount of recycalable garbage from each corporation."<<endl
    <<"3. View the total amount of non-ecycalable garbage from each corporation."<<endl
    <<"4. View the total amonut of garbage contributed."<<endl;
      cin>>Option;


//association the object logs with the file named Data.txt
     ifstream Logs;
   Logs.open("Data.txt");

   if(!Logs.is_open()){cerr<<"Sorry file could not be opened";}//error message if file is not opened

   else{


           while(!Logs.eof()){//for loop to read all the contents of the file

              //storing all the information in the appropriate array

             getline(Logs,Date[i],',');
             getline(Logs,RegionalCop[i],',');
             Logs>>Typeog[i];
             Logs.ignore(1);
             Logs>>Weightin[i];
             Logs.ignore(1);
             Logs>>Weightout[i];
            // Logs.ignore(1);
                ++i;
             }
            Logs.close();


//Switch statements for options
switch (Option){

         case 1:

           //if user chooses option 1 all the contentes of the file will be place in a table under its category and then displayed on the screen

              cout<<setw(9)<<"Date"<<setw(25)<<"  Regional corporation"<<setw(20)<<"Type of Garbage"<<setw(10)<<"weigtIn"<<setw(10)<<"weigtOut"<<endl;
               for (i=0; i<=7; i++){
              cout<<setw(9)<<Date[i]<<setw(25)<<RegionalCop[i]<<setw(20)<<Typeog[i]<<setw(10)<<Weightin[i]<<setw(10)<<Weightout[i]<<setprecision(2)<<fixed;

                                  }
                                  break;

 //if user chose option 2 the program is suppose to compare the two arrays if array regionalCop and Typeofgarbage are met the program is suppose to
 //get the total amount of recycleble garbage for the given corporation this is done by subtracting weightin - weightour of the given corporation
 //this calculation has to be repeated four times each time changing the given corporation and at the end the program is suppose to display the total amount of recyclyclabel
 //garbage for each corporation
  case 2:
                      for (int i = 0; i<=7; i++){
                  if ((Typeog[i]=='r')&& (RegionalCop[i]=="Tunapuna-Piarco")){
                       for (int i = 0; i<=7; i++){
                     Tuna = Weightin[i] - Weightout[i];
                     TunaTOT += Tuna;
                     }
                  }

               }

                   for (int i = 0; i<=7; i++){
                 if ((Typeog[i]=='r')&& (RegionalCop[i]=="San Juan-Laventille")){
                       for (int i = 0; i<=7; i++){
                     SanJuan = Weightin[i] - Weightout[i];
                     SanToT += SanJuan;
                     }
                  }
               }

     for (int i = 0; i<=7; i++){
                 if ((Typeog[i]=='r')&& (RegionalCop[i]=="Arima")){
                       for (int i = 0; i<=7; i++){
                     Arima = Weightin[i] - Weightout[i];
                     ArimaToT += Arima;
                     }
                  }
               }

            for (int i = 0; i<=7; i++){
                if ((Typeog[i]=='r')&& (RegionalCop[i]=="Chaguanus")){
                       for (int i = 0; i<=7; i++){
                     Chaguanus = Weightin[i] - Weightout[i];
                     ChaguanusTOT += Chaguanus;
                     }
                  }
               }



   cout<<"CORPORATION"<<"       "<<"   TOTAL AMOUNT OF RECYCLABLE GARBAGE"<<endl;

   cout<<"Saun Juan-Laventille   "  <<SanToT <<endl;
   cout<<"Tunapuna-Piarco        "  <<TunaTOT <<endl;
   cout<<"Arima                  "  <<ArimaToT <<endl;
   cout<<"Chaguanus              "  <<ChaguanusTOT<<endl;

          break;

  case 3:
    for (int i = 0; i<=7; i++){
                  if ((Typeog[i]=='g')&& (RegionalCop[i]=="Tunapuna-Piarco")){
                       for (int i = 0; i<=7; i++){
                     Tuna = Weightin[i] - Weightout[i];
                     TunaTOT += Tuna;
                     }
                  }

               }

                   for (int i = 0; i<=7; i++){
                 if ((Typeog[i]=='g')&& (RegionalCop[i]=="San Juan-Laventille")){
                       for (int i = 0; i<=7; i++){
                     SanJuan = Weightin[i] - Weightout[i];
                     SanToT += SanJuan;
                     }
                  }
               }

     for (int i = 0; i<=7; i++){
                 if ((Typeog[i]=='g')&& (RegionalCop[i]=="Arima")){
                       for (int i = 0; i<=7; i++){
                     Arima = Weightin[i] - Weightout[i];
                     ArimaToT += Arima;
                     }
                  }
               }

            for (int i = 0; i<=7; i++){
                if ((Typeog[i]=='g')&& (RegionalCop[i]=="Chaguanus")){
                       for (int i = 0; i<=7; i++){
                     Chaguanus = Weightin[i] - Weightout[i];
                     ChaguanusTOT += Chaguanus;
                     }
                  }
               }



   cout<<"CORPORATION"<<"       "<<"   TOTAL AMOUNT OF RECYCLABLE GARBAGE"<<endl;

   cout<<"Saun Juan-Laventille   "  <<SanToT <<endl;
   cout<<"Tunapuna-Piarco        "  <<TunaTOT <<endl;
   cout<<"Arima                  "  <<ArimaToT <<endl;
   cout<<"Chaguanus              "  <<ChaguanusTOT<<endl;







    break;














  case 4:
                   for (int i = 0; i<=7; i++){
                  if ( (RegionalCop[i]=="Tunapuna-Piarco")){
                       for (int i = 0; i<=7; i++){
                     Tuna = Weightin[i] - Weightout[i];
                     TunaTOT += Tuna;
                     }
                  }

               }

                   for (int i = 0; i<=7; i++){
                 if (RegionalCop[i]=="San Juan-Laventille"){
                       for (int i = 0; i<=7; i++){
                     SanJuan = Weightin[i] - Weightout[i];
                     SanToT += SanJuan;
                     }
                  }
               }

     for (int i = 0; i<=7; i++){
                 if (RegionalCop[i]=="Arima"){
                       for (int i = 0; i<=7; i++){
                     Arima = Weightin[i] - Weightout[i];
                     ArimaToT += Arima;
                     }
                  }
               }

            for (int i = 0; i<=7; i++){
                if (RegionalCop[i]=="Chaguanus"){
                       for (int i = 0; i<=7; i++){
                     Chaguanus = Weightin[i] - Weightout[i];
                     ChaguanusTOT += Chaguanus;
                     }
                  }
               }



   cout<<"CORPORATION"<<"       "<<"   TOTAL AMOUNT OF RECYCLABLE GARBAGE"<<endl;

   cout<<"Saun Juan-Laventille   "  <<SanToT <<endl;
   cout<<"Tunapuna-Piarco        "  <<TunaTOT <<endl;
   cout<<"Arima                  "  <<ArimaToT <<endl;
   cout<<"Chaguanus              "  <<ChaguanusTOT<<endl;

          break;






         }

  }








cout<<"\nDo you want to continue (Y/N)?"<<endl;
cin>>c;
}while(c=='y'||c=='Y');


system("PAUSE");
return 0;
   }

























Last edited on
To debug this type issue, cout each value before and after your statements.

1
2
3
4
5
6
7
8
// Example
cout << Tuna;
cout << Weightin[i];
cout Weightout[i];
                     Tuna = Weightin[i] - Weightout[i];
cout << Tuna;
cout << Weightin[i];
cout Weightout[i];


Maybe you can read your code the way it's written but I can't.
Maybe that is the way they teach you to format your code, but I recommend something like this:
Line up your {} with the statements in each loop.
Personal preference, to me it's easier to read.
Of course if your code works, then it doesn't matter.
1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i<=7; i++)
{
    if ((Typeog[i]=='g')&& (RegionalCop[i]=="Tunapuna-Piarco"))
    {
        for (int i = 0; i<=7; i++)
           {
           Tuna = Weightin[i] - Weightout[i];
           TunaTOT += Tuna;
           }
    }
}


I have not ran this but since you asked, the problem I see with this is that you have the value of "i" being changed within your loop.

Try running a test and cout i; to be sure the values are what you expect. If it was me I would change the second loop to for (int j=0).

OK i will :D thanks for you time
Topic archived. No new replies allowed.