# Problem with IF statement!

This is a discussion on Problem with IF statement! within the C++ Programming forums, part of the General Programming Boards category; Hey guys and gals! so currently my code is reading a txt file something like this. 19880113041524 408 19880113043027 406 ...

1. ## Problem with IF statement!

Hey guys and gals!

so currently my code is reading a txt file something like this.

19880113041524 408
19880113043027 406
19880113044527 406
19880113050027 406
19880113051527 404
19880113053027 402
19880113054527 402
19880113060027 400
19880113061530 399
19880113063030 399

it extracts the numbers into another array so each row looks like
1988 1 13 4 ... etc
However, when my program reads the txt at one point, it compares 1988 with 1988 and says its false. Anyone know what I'm sucking at?

Code:
```#include <stdio.h>                                                                          /* required for file operations */
#include <dos.h>                                                                            /* for delay */
#include <iostream>
#include <cstdio>
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip>
#include <cmath>
using namespace std;

main(){

FILE *fr;                                                                                // declare the file ponter for imported file
ofstream myfile;                                                                         // declare the file pointer for exported file
myfile.open ("c:\\CCY_fullNEW.txt");
int x, y;                                                                                // Dimensions of .txt data, 3 columsn and 330770 inputs;

//DYNAMIC MEMORY ALLOCATION FOR data ARRAY and finalData ARRAY.
double** data;                                                                              //allocation for input array
data = (double **) malloc(673296 * sizeof(double*));
for (int h = 0; h < 673295; h++)
data[h] = (double*) malloc(2*sizeof(double));

double** finalData;                                                                         //allocation for final data array
finalData = (double **) malloc(673295 * sizeof(double*));
for (int h = 0; h < 673295; h++)
finalData[h] = (double*) malloc(6*sizeof(double));

double** out_DATA;                                                                         //allocation for output data array
out_DATA = (double **) malloc(673295 * sizeof(double*));
for (int h = 0; h < 673295; h++)
out_DATA[h] = (double*) malloc(5*sizeof(double));
//opens the exporting file
//determines the placement of the finalData
fr = fopen ("c:\\CCY_full.prn", "r");                                                    /* reads the file */
for(x=0; x<10; x++){                                                                 // loops through the rows
for(y = 0; y <2; y++){                                                          //starts at first column during each row loop
fscanf(fr,"%Lf", &data[x][y]);
//cout.precision(0);
//cout<<fixed<<data[x][y]<<"    ";
}
//cout<<endl;                                                              // loops through the columns until each row ends.
}

//Declaring time variables
double EVENT;                                                                       //extracts and holds the 15min intervals
double DAY;                                                                         //extracts and holds the day
double test1;
double test2;
double test3;
double MONTH;                                                                       //extracts and holds the month
double YEAR;                                                                        //extracts and holds the year
double DISCHARGE;
double HOUR;
double testwut;

//This For loop extracts the parts of the large number to seperate it into specific pieces of time
for (x = 0; x<10; x++){
EVENT = fmod(data[x][0]/100, 100);
finalData[x][4] = EVENT;
test1 = EVENT * 100;

HOUR = fmod((data[x][0] - test1)/10000, 100);
finalData[x][3] = HOUR;
testwut = test1 + (HOUR * 10000);

DAY = fmod((data[x][0] - testwut)/1000000, 100);
finalData[x][2] = DAY;
test2 = testwut + (DAY * 1000000);

MONTH = fmod((data[x][0] - test2)/100000000, 100);
finalData[x][1] = MONTH;
test3 = test2 + (MONTH * 100000000);

YEAR = fmod(((data[x][0]/10) - (test3/10))/1000000000, 10000);
finalData[x][0] = YEAR;

DISCHARGE = data[x][1];
finalData[x][5] = DISCHARGE;
//cout<<finalData[x][0]<<"   "<<finalData[x][1]<<"  "<<finalData[x][2]<<"  "<<finalData[x][3]<<"   "<<finalData[x][4]<<"  "<<finalData[x][5]<<endl;
}

//cout<<finalData[8299][0]<<"   "<<finalData[8299][1]<<"   "<<finalData[8299][2]<<"   "<<finalData[8299][3];

/*for(x=0; x<10; x++){                                                               // Loops through YEAR, MONTH, DAY, EVENT;
for(y = 0; y <6; y++){
cout<<finalData[x][y]<<"  ";
}
cout<<endl;
}*/

double COUNT = 1; //determines number of inputs for total/count
double TOTAL = 408;
int z = 0;

for (x = 1; x<10; x++){ // be careful of previous data points
//cout<<finalData[x][0]<<"   "<<finalData[x-1][0]<<endl;
if (finalData[x][0] == finalData[x-1][0]){    //THIS IS WHERE THE IF STATEMENT SOMETIMES FAILS
YEAR = finalData[x][0];
if (finalData[x][1] == finalData[x-1][1]){
MONTH = finalData[x][1];
if (finalData[x][2] == finalData[x-1][2]){
DAY = finalData[x][2];
if (finalData[x][3] == finalData[x-1][3]){
HOUR = finalData[x][3];
COUNT = COUNT + 1;
TOTAL = TOTAL + finalData[x][5];
} else{
out_DATA[z][4] = TOTAL/COUNT;
out_DATA[z][0] = YEAR;
out_DATA[z][1] = MONTH;
out_DATA[z][2] = DAY;
out_DATA[z][3] = HOUR;
//cout<<COUNT<<"  "<<HOUR<<"   "<<TOTAL<<endl;
myfile <<out_DATA[z][0]<<"  "<<out_DATA[z][1]<<"  "<<out_DATA[z][2]<<"  "<<out_DATA[z][3]<<"  "<<out_DATA[z][4]<<endl;
z = z + 1;
COUNT = 0;
TOTAL = 0;
if (finalData[x][3] != (finalData[x-1][3] + 1)){
out_DATA[z][4] = -9999;
out_DATA[z][0] = YEAR;
out_DATA[z][1] = MONTH;
out_DATA[z][2] = DAY;
out_DATA[z][3] = HOUR + 1;
myfile<<out_DATA[z][0]<<"  "<<out_DATA[z][1]<<"  "<<out_DATA[z][2]<<"  "<<out_DATA[z][3]<<"  "<<out_DATA[z][4]<<endl;
z = z + 1;
}
}
} else{
out_DATA[z][4] = TOTAL/COUNT;
out_DATA[z][0] = YEAR;
out_DATA[z][1] = MONTH;
out_DATA[z][2] = DAY;
out_DATA[z][3] = HOUR;
myfile <<out_DATA[z][0]<<"  "<<out_DATA[z][1]<<"  "<<out_DATA[z][2]<<"  "<<out_DATA[z][3]<<"  "<<out_DATA[z][4]<<endl;
z = z + 1;
COUNT = 0;
TOTAL = 0;
}
}else{
out_DATA[z][4] = TOTAL/COUNT;
out_DATA[z][0] = YEAR;
out_DATA[z][1] = MONTH;
out_DATA[z][2] = DAY;
out_DATA[z][3] = HOUR;
myfile <<out_DATA[z][0]<<"  "<<out_DATA[z][1]<<"  "<<out_DATA[z][2]<<"  "<<out_DATA[z][3]<<"  "<<out_DATA[z][4]<<endl;
z = z + 1;
COUNT = 0;
TOTAL = 0;
}
} else{
out_DATA[z][4] = TOTAL/COUNT;
out_DATA[z][0] = YEAR;
out_DATA[z][1] = MONTH;
out_DATA[z][2] = DAY;
out_DATA[z][3] = HOUR;
cout<<out_DATA[z][0]<<"  "<<out_DATA[z][1]<<"  "<<out_DATA[z][2]<<"  "<<out_DATA[z][3]<<"  "<<out_DATA[z][4]<<endl;
z = z + 1;
COUNT = 0;
TOTAL = 0;
}
}
free(data);
free(finalData);
fclose(fr);
/* close the file prior to exiting the routine */
system("pause");
}```

Thank you!

2. If you try the text file out with the program, line 99 returns false when it should be true. I even display the two numbers before I check them and they look the same to me! Thanks again!

3. I wouldn't use doubles to do such calculations. I would expect rounding errors after all this fmod() s and divisions.

You could print the data with high prcision (say 15 decimals ) to see if there are any rounding errors

Kurt

4. Originally Posted by ZuK
I wouldn't use doubles to do such calculations. I would expect rounding errors after all this fmod() s and divisions.

You could print the data with high prcision (say 15 decimals ) to see if there are any rounding errors

Kurt
Awesome! I checked with a precision of 8 before and it was 1988 vs 1988. I just checked with a precision of 15 and it was 1987.99999999999880. I now just have to change those calculations to integers.

5. I fixed the problem by just rounding all the values except for the discharge. Thanks Kurt!

6. This should be moved to C++ board

7. The OP also needs to decide whether they're writing C or C++.
As it stands, it's just a big mash-up of everything which falls under "works for me".

Code:
```#include <stdio.h>                                                                          /* required for file operations */
#include <dos.h>                                                                            /* for delay */
#include <iostream>
#include <cstdio>
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip>
#include <cmath>```
Try deleting all the red ones, and change blue to be #include <fstream>
But as noted in posts above, you could probably do without all those fmod() calls, and also remove cmath as well.

Code:
```   double** data;                                                                              //allocation for input array
data = (double **) malloc(673296 * sizeof(double*));
for (int h = 0; h < 673295; h++)
data[h] = (double*) malloc(2*sizeof(double));```
In C++, use new
Like so.
Code:
```const int asize = 673296;  // always declare const's, don't spray magic numbers through the code.
data = new double*[asize];
for ( int h = 0; h < asize ; h++ )
data[h] = new double[2];```
> free(data);
This is a massive memory leak!
You need to free each data[i] before freeing data itself.
And since we're using C++, use the delete method.
Code:
```for ( int h = 0; h < asize ; h++ )
delete [] data[h];
delete [] data;```

8. While we're at it, the OP should learn to:
- Indent properly.
- Make main return int.
- Use vector instead of heavy memory allocations where tons of memory leaks will happen.

9. Magic number (programming) - Wikipedia, the free encyclopedia

The use of Unnamed numerical constants is also not a good thing.

Tim S.