d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With A Simple C++ Program
Prev12
Add Reply New Topic New Poll
Member
Posts: 27,475
Joined: Jul 5 2007
Gold: 2,943.00
Mar 14 2013 10:10pm
Quote (carteblanche @ Mar 14 2013 11:02pm)
fscanf(trains,"%i %i %i %i %i",&TrainID,&m,&iveloc,&fveloc,&d);

you mean this? i assume %i means integer, but it looks like your file contains floats. is that the problem?


I switched it up so that

my variables are:
double a,f, iv, fv;
int ndata,i,TrainID, m, d;

and my scan and print is now:
fscanf(trains,"%4i %6i %4.1f %4.1f %6i",&TrainID,&m,&iv,&fv,&d);
printf("%4i %6i %4.1f %4.1f %4i %9.1f \n",TrainID,m,iv,fv,d,f);


Also, this needs to be fixed within 45 minutes
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2013 11:02pm
So how'd you fix it?
Member
Posts: 27,475
Joined: Jul 5 2007
Gold: 2,943.00
Mar 14 2013 11:19pm
Quote (carteblanche @ Mar 15 2013 12:02am)
So how'd you fix it?


I didn't
Retired Moderator
Posts: 21,073
Joined: Apr 7 2008
Gold: 5,135.90
Trader: Trusted
Mar 15 2013 12:09am
I think it is a problem with reading the file. Give me a few mins.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 15 2013 12:14am
should be using %d or %f respectivly ive never seen %i used before.

This post was edited by AbDuCt on Mar 15 2013 12:15am
Retired Moderator
Posts: 21,073
Joined: Apr 7 2008
Gold: 5,135.90
Trader: Trusted
Mar 15 2013 12:21am
Code
/**********************************************************************
      ENGR 200 DEDP                                      DATE: 03/10/2013      
   
      Test: 2 - Stopping force of train                Author:
   ***********************************************************************
   
      PROGRAM DESCRIPTION:
      This program calculates the stopping force for 5 individual trains
      given varying speeds and distances traveled.
   
      DESCRIPTION OF VARIABLES:
      NAME     |  TYPE  | DESCRIPTION
      --------------------------------------------------------------------
      TrainID  | int    | Train ID number
      m        | int    | mass of train
      fveloc   | double | final velocity of train
      iveloc   | double | initial velocity of train
      a        | double | acceleration of train
      d        | int    | distance traveled by train
      f        | double | Stopping force for train
      ndata    | int    | number of entries
      quit     | char   | Exit the Program
      i        | int    | An integer
   ***************************************************************************************/
     
   /* Preprocessor directives */
   #include <stdio.h>
   #include <math.h>
   #define inputfile "trains.txt"
   #define outputfile "trains_result.txt"
   
   /* Main Function */
   int main(void)
   {
   
           /* Declare Variables */
           double a,f, iveloc,fveloc;
           int ndata,i,TrainID, m, d;
           char quit;
           FILE *trains, *train_results;
   
           /* Open Files */
           trains = fopen(inputfile,"r");
           train_results = fopen(outputfile,"w");
   
           /* Scan to Check for File */
   
           /* Scan to check for file */
           if(trains == NULL)
       {
                   printf("\n\nError opening input file occurred\n\n");
                   printf("\n\nProgram Terminated\n\n");
       }
                   else
           {
         
                           /* Print Headings */
                           printf("Stopping Force Program\n");
                           printf("by Luke Wagner\n\n");
                           printf("Train ID    Mass    I-Velocity    F-Velocity    Distance    Force \n");
                           printf("           (lbs)     (ft/sec)      (ft\\sec)        (ft)      (lbs) \n");
                         
                           /* Print to file */
                           fprintf(train_results,"Stopping Force Program\n");
                           fprintf(train_results,"by Luke Wagner\n\n");
                           fprintf(train_results,"Train ID    Mass    I-Velocity    F-Velocity    Distance    Force \n");
                           fprintf(train_results,"           (lbs)     (ft/sec)      (ft\\sec)        (ft)      (lbs) \n");
   
                           /* Scan File */
                           fscanf(trains,"%i",&ndata);
                           for(i=1;i<=ndata;i++)
                       {
   
                                   /* Store Data */
                                   fscanf(trains,"%i %i %lf %lf %i",&TrainID,&m,&iveloc,&fveloc,&d);
                                   
                                   
                                   /* Calculations */
                                   a=(pow(fveloc,2.0)-pow(iveloc,2.0))/(2.0*d);
                               f=(m*a);
   
                                   /* Print results */
                                 printf("%d %12d %11f %14f %10d %lf\n",TrainID,m, iveloc, fveloc,d,f);
                                fprintf(train_results,"i   %d %12d %11f %14f %10d %lf\n",TrainID,m,iveloc,fveloc,d,f);
                           }      
                           fclose(train_results);
                           /* Quit */
                                   printf("\n\n\nEnter Q TO QUIT. \n");
                                   do
                                   {
                                           quit = getchar();
                                   }
                                           while (quit != 'q');
                                           return 0;
                   }
   }
   
   /***********************************************************************************************/


This should work 100% now.

I changed up some of the values (floats, doubles etc) and some of the value that it read in(using %lf and %d like Abduct said)
As long as your calculation works it seems ok.

Output to console looks good, same with output to a file.

As a general note though make sure you close a file when you are done with it, otherwise its just a blank file when you go to open it.

Oh and if you copy this you need to change the infile and outfile back to what you had.

This post was edited by Kagura on Mar 15 2013 12:34am
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll