loop problem

This is a discussion on loop problem within the C Programming forums, part of the General Programming Boards category; Hello, I have to find the greatest number a user enters. When the user enters a ) or a number ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    loop problem

    Hello,

    I have to find the greatest number a user enters.
    When the user enters a ) or a number smaller then 0 then the output has to stop,
    So I made this :
    Code:
    
    #include        <stdio.h>
    
    
    int
    main ( int argc, char *argv[] )
    {
            float getal, grootste  ;
            getal = grootste = 1 ;
            do
            {
                    printf("Enter a number : ");
                    scanf ("%f", &getal);
                    printf ("Ingevoerde getal : %f", getal) ;
            }
            while (getal <=0);
            printf ("Het grootste getal is %f", grootste);
            return 0 ;
    }                               /* ----------  end of function main  ---------- */
    But when the user enters one number the loop stops.
    What is my thinking error here ?

    Roelof

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are confusing less than with greater than.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Thank you.
    The while must be while (getal > 0)

    Still one question:

    When I enter the numbers : 60, 38.3, 4.89, 100.62, 75.2295 I get as output 100.620003 instead of 100.62.

    Is there a reason for that ?

    Roelof

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Floating point - Wikipedia, the free encyclopedia
    Yes, all floating point numbers are approximations.
    If your float does not have an exact representation, you end up with the nearest approximate value instead.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    230
    oke,

    I read the page you told me.
    So I cannot do anything about it.

    Can I then make the conclusion that floats are not good for calculations.
    When a number has no exact represantation then it will be approximate.
    So that round up can make the outcome otherwise then expected.

    Roelof

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by roelof View Post
    oke,

    I read the page you told me.
    So I cannot do anything about it.

    Can I then make the conclusion that floats are not good for calculations.
    Roelof
    No, that is not a good conclusion to draw.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    230
    O,

    Why not.
    If 100.62 will be 100.620003 and I will multiple the number by itself then the answer would be 10124.38500372 instead of 10124.3844
    Or if I want to compare they will not be the same.

    Roelof

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Here is a short read -> Floating Point
    And a long read -> What Every Computer Scientist Should Know About Floating-Point Arithmetic
    If you really care about rounding errors and so forth, then good use of floating point is quite hard work. You have to manage those accumulated errors in some way.

    Simple answer - multiply everything by 100, so you're doing 10062 * 10062.
    Accurate, but you might run out of integer head room (overflow), and you have to keep track of where the decimal point should be.

    Less simple answer -> Fixed-point arithmetic - Wikipedia, the free encyclopedia
    Easier in C++, since you can overload all the operators in a useful way, and make it seem like nothing unusual is happening in the code.

    Meh, this is too hard, I'll get a maths library, say The GNU MP Bignum Library
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    O,

    Why not.
    If 100.62 will be 100.620003 and I will multiple the number by itself then the answer would be 10124.38500372 instead of 10124.3844
    Or if I want to compare they will not be the same.

    Roelof
    It's not a good conclusion to make simply because it is too broad. You can certainly compare decimal approximations, and make two of them the same. You just need to know that in order to be the same, you're going to consider certain digits important and ignore the rest. Most floating point operations will not be too bad unless you really shouldn't have used floating point to begin with because you needed integers instead.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Oke,

    I will study it.
    Everyone thanks for the help and patience.

    Roelof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in for loop
    By jacobsh47 in forum C Programming
    Replies: 14
    Last Post: 04-14-2011, 03:39 PM
  2. Problem with loop.
    By Squish in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2010, 10:21 AM
  3. do...while loop problem
    By geoffrey in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2010, 03:49 PM
  4. Problem in the loop
    By PunchOut in forum C Programming
    Replies: 2
    Last Post: 11-30-2008, 03:17 PM
  5. Problem with while loop
    By Fyodorox in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2002, 12:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21