Thread: trying to stop printing a table and insert a statement when the number turns neg.

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    6

    trying to stop printing a table and insert a statement when the number turns neg.

    Code:
    /* Preprocessor directives */
    #include <stdio.h>
    #include <math.h>
    #define input_file "c:\\engr 200\\train.txt"
    #define output_file "c:\\engr 200\\train_report.txt"
    
    
    /* Define the structure to represent a tsunami */
    struct trains
    {
       double time, displace, velocity;
    };
    /* Main Function */
    int main(void)
    {
       /* Declare and initialize variables */
       struct trains tr[51];
       int nrows,ncols, i, j;
       FILE *train, *train_report;
    
    
       /* Open input file */
       train = fopen(input_file,"r");
       train_report = fopen(output_file,"w");
       
       /* Verify input file, read control number, and hurricane data */
       if(train == NULL)
       {
          printf("\n\n\n\n   ERROR OPENING INPUT FILE.");
          printf("\n\n   PROGRAM TERMINATED.\n\n\n");
          return 0;
       }
       else
       {
          /* Read control number */
          fscanf(train,"%i %i",&nrows,&ncols);
          
          /* Read train data into array */
          for(j=0; j<=ncols-1; j++)
          {
            for(i=0; i<=nrows-1; i++)
                {
    			fscanf(train,"%lf %lf %lf",&tr[i].time, &tr[i].displace,
    			 &tr[i].velocity);
    		    }
          }
       }
    
    
       /* calculations */
          for(i=0; i<=nrows-1; i++)
          {
             for(j=0; j<=ncols-1; j++)
                {
    			   tr[i].displace = 4.219*(exp(-1.58*tr[i].time)-
    			        exp(-6.32*tr[i].time));
    			   
                   tr[i].velocity = 26.67*exp(-6.32*tr[i].time)-
    			        6.67*exp(-1.58*tr[i].time);
                }
           }
      
          /* Print headings */
       printf("************ START REPORT ***********"
              "\n\n     TRAIN CAR STOPPING ANALYSIS"
    		  "\n\n\n  Time      Displacement     Velocity"
    		  "\n   (s)           (m)           (m/s)");
       fprintf(train_report,"************ START REPORT ***********"
              "\n\n     TRAIN CAR STOPPING ANALYSIS"
    		  "\n\n\n  Time      Displacement     Velocity"
    		  "\n   (s)           (m)           (m/s)");
    	  
       /* Print array */
       for(i=0; i<=nrows-1; i++)
          {
          if(tr[i].velocity<0)
    	    {printf("\nThe train car stopped moving forward at %6.3f seconds and"
    		       " %6.3f meters.",tr[i].time,tr[i].displace);	
    	    }
    	else
    	{  
          printf("\n%6.3f         %6.3f         %6.3f",tr[i].time,
    	         tr[i].displace,tr[i].velocity);
    	  fprintf(train_report,"\n%6.3f         %6.3f         %6.3f",tr[i].time,
    	         tr[i].displace,tr[i].velocity);
        }
          }
       
       /* Print end */
       printf("\n\n************ END REPORT *************\n\n\n");
       fprintf(train_report,"\n\n************ END REPORT *************\n\n\n");
       
       /* Close the input file */
       fclose(train);
       fclose(train_report);
    
    
       /* Exit the program */
       return 0;
    }
    /******************************************************************************/

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,763
    Code:
          for(j=0; j<=ncols-1; j++)
          {
            for(i=0; i<=nrows-1; i++)
                {
                fscanf(train,"%lf %lf %lf",&tr[i].time, &tr[i].displace,
                 &tr[i].velocity);
                }
          }
       }
    In your other post, your example file had 51 rows of 3 numbers.
    Since you read in 3 numbers at a time here, you only need to loop 51 times.

    But with nested loops, it's 153 times.

    The last 102 values in your array contain garbage.

    Try this
    Code:
          for(j=0; j<=ncols-1; j++)
          {
            for(i=0; i<=nrows-1; i++)
                {
                if ( fscanf(train,"%lf %lf %lf",&tr[i].time, &tr[i].displace,
                 &tr[i].velocity) != 3 ) {
                        printf("Oops at %d %d\n", j, i );
                    }
                }
          }
       }
    This should at least serve to illustrate the important of checking for errors rather than stumbling around in the dark.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a table-like text
    By C_ntua in forum C# Programming
    Replies: 2
    Last Post: 11-16-2010, 04:35 PM
  2. Replies: 2
    Last Post: 10-18-2009, 01:05 PM
  3. Can't stop my program at zero (Loan Table)
    By beachsidefl321 in forum C Programming
    Replies: 10
    Last Post: 06-24-2009, 09:14 PM
  4. Insert Items in Hash Table Container.
    By joenching in forum C++ Programming
    Replies: 18
    Last Post: 05-02-2005, 01:50 PM
  5. Insert picture for printing
    By ooosawaddee3 in forum C Programming
    Replies: 0
    Last Post: 04-05-2002, 09:26 PM