Thread: Reading a char and converting to float

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    6

    Reading a char and converting to float

    Hi everyone,

    I'm a bit stuck on a problem. I'm trying to write a function that reads in a character at a time and converts the input to double. I have a function that works with Integers. It reads a char and converts to an int. I don't know how to, or if it is possible, to convert a char to a float/double.

    I have done a search and found methods for converting a character string to double. What I am trying to do is to do it a char at a time.

    Is there a function to detect the decimal point? I don't know.

    If you have any links or advice that would be welcome.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,997
    Is this a deliberate exercise in making it difficult for yourself?

    > I have a function that works with Integers.
    OK, so start with that.
    Valid integers are also valid floats, so typing in 1234 to either of them should still work.

    > Is there a function to detect the decimal point? I don't know.
    It seems a rather grandiose way of putting
    Code:
    if ( ch == '.' ) {
      // yay!, decimal point
    }
    > I have done a search and found methods for converting a character string to double.
    So storing the intermediate string one character at a time, and calling said "string to double" with the partial string isn't allowed?
    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 2020
    Posts
    6
    Quote Originally Posted by Salem View Post
    Is this a deliberate exercise in making it difficult for yourself?
    Nope, just doing exercises out of a book. Joy of C - Programming in C second edition, Miller, Quilici.

    Quote Originally Posted by Salem View Post
    Code:
    if ( ch == '.' ) {
      // yay!, decimal point
    }

    I actually thought of this last night as I was laying in bed. Or using a function from ctype.h

    Thank you for your reply, much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting to float
    By jturner38 in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2010, 10:08 PM
  2. Converting pointer to float.
    By coolrox86 in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2010, 10:44 PM
  3. Error if reading char or float
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-07-2007, 03:06 PM
  4. Reading in char for float with scanf
    By ramparts in forum C Programming
    Replies: 3
    Last Post: 11-05-2006, 01:05 AM
  5. Converting char * to float using sscanf()
    By caduardo21 in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 02:51 AM

Tags for this Thread