Boo92 Boo92 - 1 month ago 11
C Question

Cannot figure out why my code is not working

I've been trying to figure out what is wrong with my code for days; I can't seem to find it. If statements confuse me quite a bit, so I'm thinking I messed those up, but still not sure. All I know is that I'm super stumped and would love all of the help I can get.

#include <stdio.h>

// function main begins program execution
int main(void)
{
int numberOfDays = 0;
float numberOfMiles = 0;
float milesCharge = 0;
float milesTotal = 0;
float total = 0;
float subtotal = 0;
float tax = 0;

do {
printf("%s", "How many days was car rented?\t");
scanf("%d", &numberOfDays);
} while (numberOfDays < 1 );

do {
printf("%s", "How many miles were driven?\t");
scanf("%d", &numberOfMiles);
} while (numberOfMiles > 1);

if (numberOfMiles > 1 || numberOfMiles < 200) {
milesTotal = numberOfMiles * .40;
} else {
milesTotal = numberOfMiles * .35;
}

subtotal = milesTotal + numberOfDays * 15;
tax = subtotal * .06;
total = tax + subtotal;

printf("\nSubtotal:\t\t\t$%.2f\n", subtotal);
printf("Tax Amount:\t\t\t$%.2f\n", tax);
printf("Total:\t\t\t\t$%.2f\n", total);
printf("\n");
}

Answer

If numberOfMiles should be of type float you have to exchange the following line

scanf("%d", &numberOfMiles);

by

scanf("%f", &numberOfMiles);

or you can set the type to int.

--

If you want to avoid an endless loop, exchange

} while (numberOfMiles > 1);

by

} while (numberOfMiles < 1);

Btw. why not allow distances that are shorter than a mile?

e.g. by

} while (numberOfMiles < 0);

For more specific answers, you have to be more precise.

Comments