Thread: Problem with IF statement!

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    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!
    Last edited by turkadurka; 08-07-2012 at 01:52 PM.

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    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. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    Quote Originally Posted by ZuK View Post
    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. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    I fixed the problem by just rounding all the values except for the discharge. Thanks Kurt!

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This should be moved to C++ board
    Last edited by Click_here; 08-07-2012 at 05:51 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Magic number (programming) - Wikipedia, the free encyclopedia

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

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with if statement
    By adohertyd in forum C Programming
    Replies: 4
    Last Post: 12-20-2011, 09:34 PM
  2. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  3. problem if statement
    By irncty99 in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2003, 09:33 AM
  4. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM
  5. having a problem with a FOR statement
    By jonesy in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 01:24 PM

Tags for this Thread