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