Variable being used w/o being initialised


Hi, After compiling without errors in MS Visual Studio; when I hit Ctrl F5

and enter 2 digits to test it , I get a message that says

"variable tripdistance being used without being initialized".

I wonder if anyone could please shed any light on why this is happening, Thanks







/* MILEAGE REIMBURSEMENT CALCULATOR */

#include "stdafx.h"
#include "stdio.h"
#define rate 0.35 /* conversion constant */

int
main(void)

{

double beginning ; /* input - odometer reading at start of trip */
double end; /* input - odometer reading at end of trip */
double tripdistance; /* input - distance travelled during trip */
double rate; /* input - mileage reimbursement rate for trip */
double reimbursement; /* input - total reimbursement */


/* Subtract the beginning from the end to get the tripdistance */
tripdistance = end - beginning;

scanf("%5f%5f%5f", &end, &beginning, &tripdistance);

printf("Enter three real numbers> ");

/* Multiply the tripdistance by the rate to get the reimbursement */
reimbursement = tripdistance * rate;

scanf("%5f%5f%5f", &reimbursement, &tripdistance, &rate);

printf("%5f * %5f = %5f\n", tripdistance, rate, reimbursement);


return (0);

}

Neither 'end' or 'beginning' is initialsed the first time round so therefore 'tripdistance' is not initialised. Just put the calculations after the scanf. Also as a safety, set all your doubles to zero.
Your code does not compile. :/
#define rate 0.35 //Interferes with your declaration of rate in main.

end and beginning don't have any meaningful value when you're figuring out the value of tripdistance. Probably it would be a good idea to move that to after the first scanf. :)

Also your first printf is in a weird place considering what it's supposed to be indicating...

Why two scanfs? What's the purpose of the second one?

-Albatross
Last edited on
Topic archived. No new replies allowed.