d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With A Simple C++ Program
12Next
Add Reply New Topic New Poll
Member
Posts: 27,475
Joined: Jul 5 2007
Gold: 2,943.00
Mar 14 2013 08:03pm
For some reason my loop isn't working correctly.

If anyone can help me out with this let me know and I'll send you the script I have so far.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2013 08:11pm
Post it.
Member
Posts: 27,475
Joined: Jul 5 2007
Gold: 2,943.00
Mar 14 2013 08:23pm
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "C:\\Users\\Luke Wagner\\Desktop\\ENGR 200\\trains.txt"
#define outputfile "C:\\Users\\Luke Wagner\\Desktop\\ENGR 200\\train_results.txt"

/* Main Function */
int main(void)
{

/* Declare Variables */
double a,f;
int ndata,i,TrainID, m, d, iveloc,fveloc;
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 %i %i %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("%i %i %4i %4i %i %9i \n",TrainID,m,iveloc,fveloc,d,f);
fprintf(train_results,"i %i %4.1i %4.1i %i %9.1i \n",TrainID,m,iveloc,fveloc,d,f);
}

/* Quit */
printf("\n\n\nEnter Q TO QUIT. \n");
do
{
quit = getchar();
}
while (quit != 'q');
return 0;
}
}


The output is supposed to look like this:


And the input file is this:
5
2221 200000 75.5 25.0 2000
3582 300000 65.2 18.0 4000
3468 400000 88.0 35.0 3500
7635 150000 55.5 25.5 2500
6543 555000 65.7 9.5 3200
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 14 2013 08:33pm
Can you use some indentation for us?
Member
Posts: 27,475
Joined: Jul 5 2007
Gold: 2,943.00
Mar 14 2013 08:37pm
Quote (Xarai @ Mar 14 2013 09:33pm)
Can you use some indentation for us?


http://pastebin.com/PsmvB94L
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 14 2013 08:54pm
Quote (Skywalker @ Mar 14 2013 10:23pm)

The output is supposed to look like this:
http://i296.photobucket.com/albums/mm199/frogger_dude/output_zps7aad61f3.png

And the input file is this:
5
2221 200000 75.5 25.0 2000
3582 300000 65.2 18.0 4000
3468 400000 88.0 35.0 3500
7635 150000 55.5 25.5 2500
6543 555000 65.7  9.5 3200


have you tried debugging it? place a breakpoint one instruction before your loop and then step through your loop line by line while watching your variables.

also what does your output currently look like?

Quote (Xarai @ Mar 14 2013 10:33pm)
Can you use some indentation for us?


this made me chuckle a little bit.
Member
Posts: 27,475
Joined: Jul 5 2007
Gold: 2,943.00
Mar 14 2013 09:30pm
Quote (AbDuCt @ Mar 14 2013 09:54pm)
have you tried debugging it? place a breakpoint one instruction before your loop and then step through your loop line by line while watching your variables.

also what does your output currently look like?



this made me chuckle a little bit.


The output is one line repeated 5 times with incorrect calculations. I've narrowed it down to something wrong with the loop and the initial and final velocity calculations
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2013 09:46pm
Quote (Skywalker @ Mar 14 2013 11:30pm)
The output is one line repeated 5 times with incorrect calculations. I've narrowed it down to something wrong with the loop and the initial and final velocity calculations


for debugging, only run the loop once.

eg, instead of :
for(i=1;i<=ndata;i++)

do:
for(i=1;i<=1;i++)

then look at each line inside the loop. if everything works, change to i <= 2 and repeat. if it doesnt work, then stop and look very carefully right when it fails.
Member
Posts: 27,475
Joined: Jul 5 2007
Gold: 2,943.00
Mar 14 2013 09:57pm
Still not working.

When I debug it isn't reading my final velocity, distance, or calculating the force.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2013 10:02pm
Quote (Skywalker @ Mar 14 2013 11:57pm)
Still not working.

When I debug it isn't reading my final velocity, distance, or calculating the force.


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?
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll