I have a homework assignment to write a program about mileage. I've been able to get it to work, well except the average doesn't calculate correctly and I haven't been able to verifiy the users input. I'm using Visual C as my compiler.

Can someone help me with checking the users input and how to get the average to calculate correctly?

#include <stdio.h>

int main ()
{
/* Declare variables */
char a = 0, z = 0, A = 0, Z = 0;
float gallons, miles;
float totalGallons = 0;
float totalMiles = 0;
float tank = 0;
float average = 0;


/* Greetings and instructions to user */
printf( "\nAt the first prompt please enter the gallons used or -1 to exit program.\n"
"At the second prompt enter the miles that was driven. I will calculate\n"
"the total miles per gallon. After you type -1 to end the program, I will\n"
"give you the average of your total miles per gallon.");

/* Ask for gallons or sentinel value from user */
printf( "\n\n\nEnter the gallons used (-1 to end): ");
scanf( "%f", &gallons ); /* read users input */
if (gallons == a - z || A - Z){
fprintf( stderr, "You entered a letter. Please enter a number.\n" );

}

/* Start loop and calculation */
while ( gallons != -1 ) {

/* Ask for miles from user */
printf( "\nEnter the miles driven: ");
scanf( "%f", &miles );

/* Calculate useage */
tank = miles / gallons;
printf( "\nThe miles / gallon for this tank was: %.6f.\n\n", tank);

/* Ask user for gallons or sentinel value */
printf( "\nEnter the gallons used (-1 to end): ");
scanf( "%f", &gallons );

/* Calculate useage */
totalGallons = totalGallons + gallons;
totalMiles = totalMiles + miles;
/* Exit loop */
}

/* Calculate and print average */
average = (float)totalMiles / totalGallons;
printf( "\n\nThe overall average miles/gallon was: %.6f.\n\n", average);

/* Exit program */
return 0;
}

Recommended Answers

All 5 Replies

>if (gallons == a - z || A - Z){
There are several things wrong with this. First, you're testing the value of characters, so you need to be surrounding them with single quotes. Next, you need to recompare with every part of the expression. C isn't polite enough to assume what you're doing. Third, your logic is faulty. And finally, testing gallons against a character is doomed to failure because it's a float. If it were a character, you could do this:

if ( gallons >= 'a' && gallons <= 'z' ||
  gallons >= 'A' && gallons <= 'Z' )
{

However, in reality, you have two choices to validate a float. First, you can use the return value of scanf, which is (put simply) the number of successfully read values matching the format string:

if ( scanf ( "%f", &gallons ) != 1 ) {
  // Invalid input
}

The second way is much better. Read all input as a string and validate it while in memory. Then when you're sure that it's legit, you convert to the appropriate type. That's probably too advanced of a solution at this point, so you'll probably want the scanf solution.

You are not calculating the total gallons after first gallon input before while, and after finishing the prog you are substacting from total gallons -1.

Thank you very much Narue for your reply. I did try what you said, but I just got more error messages. I think I must of done something wrong but I'm not smart enough to know what. This is what I wrote:

/* Ask for gallons or sentinel value from user */
printf( "\n\n\nEnter the gallons used (-1 to end): ");
if scanf( "%f", &gallons ) !=1) {
// Invalid input
}/* read users input */

/*if (gallons == a - z || A - Z){
fprintf( stderr, "You entered a letter. Please enter a number.\n" );
break;
}

/* Start loop and calculation */
while ( gallons != -1 ) {

And this is one of the 10 error messages I got:

error C2061: syntax error : identifier 'scanf'.

Luckly it's not a requirement to check the users input in this program, but I thought it would be a good habit to get into when writing any type of program.

I still have to figure out why my average value is coming out wrong. I don't have a counter in this program, but I'm starting to think that if I did, maybe that would help me to get the correct average value, since I don't have a set number of times the program is going to run. What do you think? What would be the best way to figure out the average?

Thanks again for your help.
Little E

>error C2061: syntax error : identifier 'scanf'
it means you need ( before scanf.
>I still have to figure out why my average value is coming out wrong.
Did U read my post?
>What would be the best way to figure out the average?
Accumulating the miles / gallons and then dividing with number which represent how many times miles / gallons were calculated.

// dont forget to initialise totalTank to zero
while(numberOfTime)
{
   tank = miles / gallon;
   totalTank += tank;
}
avarageTank = totalTank / numberOfTime;

Thank you andor. Yes I did read your first post, but I didn't understand it. But your second post makes sense to me and I was able to get that to work. Thank you.

Little E

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.