Convert char to float

This is a discussion on Convert char to float within the C Programming forums, part of the General Programming Boards category; is there way you can convert a char into a float.I have some code but it's not working. Code: #include ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    21

    Convert char to float

    is there way you can convert a char into a float.I have some code but it's not working.

    Code:
    #include <stdio.h>
    #include <ctype.h>
     #include <stdlib.h>
    
    int main()
    {    char ch;        
          int flag;     
    
         printf("Type text, terminate with EOF\n");
         flag = scanf("%c", &ch);        
          while (ch != '\n') {          
                     printf("%c", ch);    
                     flag = scanf("%c", &ch);   
                     float aaa=(atof("ch"));
                     printf("%1.0f",aaa);
    
         }
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    maybe you can use this link, http://www.cppreference.com/stdstring/atof.html. It seems like atof is not working the way you want it to.

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You want to convert a string to a double, not a single character.

    Read the input from the user into a char array, not a char.

    You are doing atof("ch")... The quotes mean you are passing a literal string that contains the letters c and h, not the variable ch.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Here is the related FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Option 3 seems to be the most useful.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    float aaa=(atof("ch"));
    look, atof fucntion is basically used to convert the string to float. in your case u have declared ch as single char. with the above code its really means that u are sending the litral vaue Ch to the fucntion ch. which u are really dont want that. u can use type cast thing to that.

    Code:
    #include<stdio.h>
    
    int main()
    {
        char ch;
        float res;
    
        printf("Enter a char\n?");
        scanf("%c",&ch);
        res= (float)ch;  // type casting
        printf("%f",res);
       
        getchar();
        return 0;
    }
    /*my ouput
    Enter a char
    ?g
    103.000000
    
    Enter a char
    ?4
    52.000000
    */
    ssharish2005

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    thanks to all those who are helping me. cwr we are not allowed to use arrays in our assign.ssharish2005 we are suppose to read characters but the value should not change for example if I put 1 I should get 1 back in return.dwks we are not allowed to use arrays and we have to use scanf. I am sorry if I was not clear in the requirements. Thanks for helping I am still trying to figure this out so if you have more suggestion let me know.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    is this is the one are u wanted
    Code:
    #include<stdio.h>
    
    int main()
    {
        char ch;
        float res;
    
        printf("Enter a char\n?");
        scanf("%c",&ch);
        res= (float)ch - '0';  // type casting
        printf("%f",res);
    
        getchar();
        return 0;
    }
    /*my ouput
    Enter a char
    ?8
    8.000000
    
    Enter a char
    ?6
    6.000000
    */
    ssharish2005

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    thanks. this program only reads the first char. so I implemented it in my program and now it doesnot work.

    Code:
    
    #include <stdio.h>
    #include <ctype.h>
     #include <stdlib.h>
    
    int main()
    {    char ch;
          int flag;
         float res;
         printf("Type text, terminate with EOF\n");
         flag = scanf("%c", &ch);
          while (ch != '\n') {
                     printf("%c", ch);
                     flag = scanf("%c", &ch);
                     res= (float)ch - '0';
    
                     printf("%f",res);
    
         }
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you have to accumulate the result, like this. Multiply current value by 10 to upen space for new digit.
    Code:
     float res = 0;
    ..
    ...
                     res= (res * 10.0F) + (float)(ch - '0');

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    thanks that worked!. I have one more question how can I check if the chracters entered by the user is numeber or a chracter.I have something like this

    Code:
     if( dcount>2 || (!isdigit(scanPrice)) || scanPrice!= '\n'){
       printf("Illegal entry: press enter to reenter the price .\n");
       continue;
    }
    else{
     result=aft+pre;
     fflush(stdin);
    printf ("Enter item name: ");
    scanf ("%s", &scanItem);
    printf("%s%s%f",&scanItem,"  ",result);
    	add(result,scanItem);
    			}
    numOfCharsInC = 0;
    options();
    This is not working.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    isdigit() works on a single character, not a float or, double or other data types. just use it for each character entered
    Code:
    ..
    ...
          while (ch != '\n') {
                     printf("%c", ch);
                     flag = scanf("%c", &ch);
                     if( !isdigit(ch))
                         printf("Error\n");
                     else
                         res= (res * 10) + (float)ch - '0';
                  }
    
    ...
    Last edited by Ancient Dragon; 02-16-2006 at 09:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  4. Repetition in do{}while
    By Beast() in forum C Programming
    Replies: 25
    Last Post: 06-16-2004, 10:47 PM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 09:14 PM

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