Thread: Problem with IF statement!

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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