string to float,with sign,comma,backspace

This is a discussion on string to float,with sign,comma,backspace within the C Programming forums, part of the General Programming Boards category; I`m using the following code and it works fine till the last line.atof should convert a string to float.It works ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    Question string to float,with sign,comma,backspace

    I`m using the following code and it works fine till the last line.atof should convert a string to float.It works fine with atoi(ansi to int).With the atof it turns out weird numbers in the range of thousands. What am I doing wrong? I`d appreciate any help.
    code:
    Code:
    void InputStr(char *buffer, unsigned short maxlen)
    {
      SCR_STATE ss1;
      signed short key;
      unsigned short i = 0;
      buffer[0] = 0;
      SaveScrState (&ss1);  
     do{	 
        MoveTo (ss1.CurX, ss1.CurY);       
         SaveScrState (&ss1);
          if ( i==3 ) {printf ("%s  ", buffer);}    
           else                                                 printf ("%s_  ", buffer);             
         key = ngetchx ();                          
         if (key >= '0' && key <= '9' && i < maxlen)                                            //48-57 
          buffer[i++] = key;                        
          else if (key == KEY_BACKSPACE )   i--;                                                  //257 
         else if (key==173 && i==0) buffer[i++] = key;                                              //(-) 
         else if (key==46)buffer[i++] = key;   //comma
         buffer[i] = 0;                               
       } while (key != KEY_ENTER);
    
          Input = atof(buffer);//buffer is string      
    }

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You don't like whitespaces or readability in general isn't it?

    Anyway, I assume you have included stdlib.h. What type is Input, it should be double. I see a lot of functions I don't know about, but it seems I don't have to care about that.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    21
    Actually I have nothing against white spaces and I am concerned about readability but it looks like that I`m not very good at it.Input is float type, and her`s the eintire code:Its for my TI calculator.

    Code:
    #define USE_TI89 // Compile for TI-89
    #define USE_TI92PLUS // Compile for TI-92 Plus
    #define USE_V200 // Compile for V200

    #define MIN_AMS 100 // Compile for AMS 1.00 or higher

    #define SAVE_SCREEN // Save/Restore LCD Contents

    #include <tigcclib.h> // Include All Header Files
    /**************Glob_Var*************************/
    float Input; // converted enters tring -any input
    /****************SubRoutines*********************** *********/
    void InputStr(char *buffer, unsigned short maxlen);

    /****************VoidInputstring******************* *********/
    void InputStr(char *buffer, unsigned short maxlen)
    {
    SCR_STATE ss1;
    signed short key;
    unsigned short i = 0;
    buffer[0] = 0;

    SaveScrState (&ss1);
    do{

    MoveTo (ss1.CurX, ss1.CurY);
    SaveScrState (&ss1);

    if ( i==3 ) {printf ("%s ", buffer);}
    else
    printf ("%s_ ", buffer);
    key = ngetchx ();
    if (key >= '0' && key <= '9' && i < maxlen)
    buffer[i++] = key;
    else if (key == KEY_BACKSPACE ) i--;
    else if (key==173 && i==0) buffer[i++] = key;//(-)
    else if (key==46)buffer[i++] = key; //comma
    buffer[i] = 0;

    } while (key != KEY_ENTER);
    Input = atof(buffer);


    }

    void _main(void)
    {

    int key;
    char str[7];
    do
    {
    clrscr ();
    InputStr (str,7);
    printf("\nconverted float=%3.3f",Input);
    key=ngetchx();

    }while (key!=KEY_ESC);

    }

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, 07:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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