Thread: Newbie Code question. Where did I go wrong?

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Newbie Code question. Where did I go wrong?

    Very very new to C, and any help is appreciated. I am attempting to write a basic code to get the perimeter or a triangle. When I use input of 10.2, 10.3, and 10.4 i get back 30.899998 instead of the expected 30.9. Did i make a mistake somewhere? Below is the code i used.
    Code:
    #include <stdio.h>
     
    int main ()
    {
      /* variable definition: */
      float base, sidea, sideb, perimeter;
      /* Prompt user for base */
      printf("Enter the base of the triangle: \n");
      // Input the base
      scanf("%f", &base);
      /* Prompt user for sidea */
      printf("Enter the length of sidea of the triangle: \n");
      // Input the sidea
      scanf("%f", &sidea);
        /* Prompt user for Sideb */
      printf("Enter the length of sideb of the triangle: \n");
      // Input the sidea
      scanf("%f", &sideb);
      // Calculate the Perimeter
      perimeter= (base + sidea + sideb);
      // Print the result
      printf("Perimeter is : %f\n", perimeter);
      return 0;
    }
    Thanks for the help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you might get a bit closer if you use 'double' rather than 'float'.

    But you're up against the fundamental problem that all floating points are approximations of real numbers.
    IEEE-754 Floating Point Converter

    30.9 has no true representation.
    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
    Join Date
    Mar 2018
    Posts
    2
    Thank you very much. Super helpful.

  4. #4
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Hi flecsy

    I'am too new on this board, I saw your program and type the code in and the result is 30.9000.
    The compiler is Newbie, and is freeware. I was courius, and give it a try.
    I have absolutely no idea why mine result is correct.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by practys View Post
    I saw your program and type the code in and the result is 30.9000. The compiler is Newbie, and is freeware. I was courius, and give it a try. I have absolutely no idea why mine result is correct.
    That's the same number, just rounded to only 4 decimal places. Try replacing the %f in the printf to %.6f and see what you get.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    If I use %.6f a fault in the answere, after the input 10.2 in line(base), line (sitea) and line (siteb) they become direct printed, no input for basea.
    the answere is 20.5.
    I must write %2.2f or %2.6f, then no mistakes.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by practys View Post
    If I use %.6f a fault in the answere, after the input 10.2 in line(base), line (sitea) and line (siteb) they become direct printed, no input for basea.
    the answere is 20.5.
    I must write %2.2f or %2.6f, then no mistakes.
    Don't add the %.6f everywhere. Only put it on the last line. Leave the others as %f
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    John.c
    Your statement is correct, If I use only %f, the answere is 30.900000 . If I change it to %.6f, the answere is the same, 30.900000 .
    If I change to %.1f the answere is 30.9 . This changes only in the last printf line.

  9. #9
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Flescy
    Thank you for your file. there is a small piece of C much more clear for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this loop? Newbie question plz help
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 09:54 PM
  2. Newbie question. What am I doing wrong?
    By kabuatama in forum C Programming
    Replies: 9
    Last Post: 02-04-2006, 03:43 PM
  3. Problem with code (newbie question)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2002, 01:39 AM
  4. More newbie question - code error
    By sunzoner in forum C++ Programming
    Replies: 1
    Last Post: 06-14-2002, 03:04 AM
  5. Newbie Source Code question. Hlp Plz?
    By BOOGIEMAN in forum Game Programming
    Replies: 4
    Last Post: 12-14-2001, 04:30 AM

Tags for this Thread