Thread: Help I am stuck with an unknown type of error.

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    17

    Help I am stuck with an unknown type of error.

    I'm a complete novice to C programming and also new to this type of online discussion so bear with me, help me and correct me if I'm wrong anywhere. Here is the code:
    Code:
    //this program calculates the average miles travelled per gallon of fuel.
    #include <stdio.h>
    //start of main
    int
    main (void)
    {
    
    
    float gallons, miles, average = 0.0, counter = 0.0;    //declaration/initialisation
      printf ("\t\t\t%s\n", "CALCULATE MILES PER GALLONS FOR YOUR CAR");    //message
    //start of while loop
      while (gallons != -1.0)
        {                //condition to end program
          printf ("%s", "Enter the gallons used(-1 to end): ");    //prompt
          scanf ("%f\n", &gallons);    //input
          if (gallons != -1.0)
        {            //condition to take further input; start of if
          printf ("%s", "Enter the miles driven: ");    //prompt
          scanf ("%f", &miles);    //input
          average += miles / gallons;    //summation of all averages
          counter++;        //incrementing counter
          printf ("%s%0.2f\n\n", "The miles/gallon for this tank was ", average);    //printing current average
        }            //end of if
        }                //end of while
      if (counter != 0.0)
        {                //start of if
          printf ("%s%0.2f", "The overall average miles/gallon was ", average / counter);    //printing final average
        }                //end of if
      else
    
    
    puts ("Invalid Input..");
    
    
    }                //end of main
    I know this maybe a repeat question but this program compiles without a syntax error and runs upto second printf function and than hangs or something which I'm unable to understand, what kind of error is this? I don't find any logical error in the code. Thank You.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    Remove the trailing \n on your first scanf.

    The second scanf will automatically skip over leading whitespace and newlines for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    836
    Remove the "\n" from the scanf() on line 15.

    Please clean up the indent style of your source code. Choose one style and stick with it. Some editors can be set to use one of the styles. Check the documentation for your editor.

  4. #4
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    Thank You for your help, now its running.

  5. #5
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    Thank You, now it's running. Actually I was trying to compile it online so I used this option to beautify which added this extra spaces everywhere, I usually write a very condensed code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unknown size of type 'void'
    By Engineer15 in forum C Programming
    Replies: 4
    Last Post: 04-15-2015, 01:08 PM
  2. Unknown error with Y/N type system.
    By JoshLalonde in forum C Programming
    Replies: 15
    Last Post: 02-14-2013, 04:51 PM
  3. unknown type name __m128 not enabled
    By a.mlw.walker in forum C Programming
    Replies: 7
    Last Post: 10-03-2012, 11:40 AM
  4. unknown file type reading
    By xixonga in forum C Programming
    Replies: 20
    Last Post: 11-28-2010, 12:17 PM
  5. Error-size of te type is unknown
    By as_rule in forum C Programming
    Replies: 5
    Last Post: 08-29-2010, 08:28 AM

Tags for this Thread