/* 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299`` ``````#include #include #include #include #include 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."<>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<>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.

 ``12345678`` ``````// 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.
 ``1234567891011`` ``````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.