determining floats or ints

This is a discussion on determining floats or ints within the C++ Programming forums, part of the General Programming Boards category; I am a new programmer and I am trying to write a funtion which does an equation then determines if ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    determining floats or ints

    I am a new programmer and I am trying to write a funtion which does an equation then determines if it is a float, or decimal number. If it is then it needs to be rounded up to the next whole number. Any suggestions?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I'm assuming your input is text representing a number. Attempt to convert it to a double, if that fails attempt to convert it to an int. If the first attempt fails, it's not a floating-point value; if the second attempt fails, it's not an integral type either.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    Since it will always round up could I use the ceil function in the cmath library?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Sure. But don't gloss over the input section. Actually, it might be better to check for integer before floating point since something like "1234" is both.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. efficiency comparing ints and floats
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-28-2009, 03:56 PM
  2. ints & floats
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 02-28-2009, 03:41 PM
  3. Replies: 15
    Last Post: 12-17-2008, 12:11 AM
  4. reading int's and floats...
    By flightsimdude in forum C Programming
    Replies: 8
    Last Post: 09-20-2003, 02:17 PM
  5. Replies: 2
    Last Post: 08-05-2002, 03:19 PM

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