Thread: atof, float and structs

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    Question atof, float and structs

    Hi,

    I'm new here and first one thing... is there any way that i can receive the messages from the board by e-mail???

    And now the real reason of this message...
    I have a problem using atof() and i can't undestand what's happening since i have a struct and i have used it already with floats and i had no problem... but now when the function begins i receive a message from the watch box telling it's an illegal use of floating points... ( I have tried to change the way i get the string but it didn't work anyway... i have gotten out the NULL char at the end of the string and nothing, i have changed a lot of things but nothing seems to work out... and i didn't find any useful help in the board yet... can someone help me PLEASEEEEEEEE!!!!

    This function will get the information from the keyboard to make me able of doing some operations without knowing how many arguments will be typed

    here goes the code:
    Code:
    #define ENTER 13
    #define MAXS 6
    #define INTGR 1
    #define FLT 2
    #define STRG 3
    
    
    typedef struct tokens{
            int telem;
            union { int ielem;
                    float felem;
                    char opelem;
                    } tokelem;
            }*TOKEN;
    
    
    
    TOKEN GetToken (void) {
            char c;
            TOKEN tok=NULL;
            char S[MAXS];
            int i = 0;
            float fl;
    
            c = getche();
            while ( c == ' ') c=getche();
            while ( (c != ' ' ) && (c!= ENTER) ){
                if (c>='0'&& c<='9'){
                   while ( c!=' '){
                         S[i]=c;
                         i++;
                         c=getche();
                   }
                   S[i]='/0';
                   tok = malloc (sizeof(struct tokens));
                   if (strstr(S,".") == NULL ) {
                      tok->telem = 1;
                      tok->tokelem.ielem = atoi(S);
                      }
                      else {
                          tok->telem = 2;
                          fl = atof(S);
                          tok->tokelem.felem = fl;
                          }
                return tok;
                }
                else
                    switch ( c ) {
                    case '+':
                    case '-':
                    case '/':
                    case '*': tok->telem = 3;
                              tok->tokelem.opelem = c;
                              return tok;
                    default : GetToken(); break;
                }
            }
            tok = NULL;
            return tok;
            }

    Code Tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @frigga: Where does it say the error is? Can you cut/paste the error message?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    17
    the error happens when i'm running it when it tries to convert the string to float (you can see i even tried to get the value in another float to see if the problem was in my struct)


    when running

    fl = atof(S);

    it gives me a message: scanf: floating point formats not linked
    Abnormal program termination

    i'm using Turbo C 2.01 and i don't know to fix it really :o(

    I have seen a message telling about it after i had posted this message but i don't know how to fix it :o/

    (I think i need another compiler... :o/)

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Reflective Factory Design
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 12-16-2005, 06:50 PM
  3. Pointer
    By silicon in forum C Programming
    Replies: 2
    Last Post: 03-25-2004, 02:34 PM
  4. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM
  5. Help with pointers and structs
    By NewToC in forum C Programming
    Replies: 8
    Last Post: 12-13-2002, 07:04 AM