Thread: atof error

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    atof error

    Hey guys does anyone know why in this function i get an error at line 401 i think its at the atof function



    Code:
    void addStock(TennisStoreType* ts)
    {
      char tmpDescription[DESCRIPTION_MAX + 1];
      char tmpunitPricePtr[10];
      char tmpstockLevelPtr[PRICE_COLWIDTH];
      
      float tmpPrice;
      int tmpStock[STOCKLEVEL_MAX];
      int finished = FAILURE;
    
      StockNodeType* curStock = ts -> headStock;
      StockNodeType* newStock;
      StockNodeType* prevStock;
     
       do
      {
         printf("Description: (1-40 characters)\n");
         fgets(tmpDescription, DESCRIPTION_MAX + 2, stdin);
    
         /* check the range of surname */
    
         if(tmpDescription[strlen(tmpDescription) -1] != '\n');
         {
            printf("Description too long less that 12\n\n");
         }     
    
         /* fails if empty line is pressed*/
    
         if(tmpDescription[0] == '\n')
         {
            printf("\t\n*****back to main menu*****");
            finished = FAILURE;
         }
    
      }while(tmpDescription[strlen(tmpDescription)-1] != '\n');
    
      tmpDescription[strlen(tmpDescription)-1] = '\0';
      
      /**** Unit Price ****/
    
       do
      {
         printf("UnitPrice: (0.05-999.95)\n");
         fgets(tmpunitPricePtr, POSTCODE_LEN + 2, stdin);
    
         /* fails if empty line is pressed*/
    
         if(tmpunitPricePtr[0] == '\n')
         {
            printf("\t\n*****back to main menu*****");
            finished = FAILURE;
         }
    
    
         /* check the range of the unit pice */
    
         if(tmpunitPricePtr[strlen(tmpunitPricePtr) -1] != '\n')
         {
            printf("The unit price is too long!\n\n");
            readRestOfLine();
         }
    
         else
         {
            tmpunitPricePtr[strlen(tmpunitPricePtr) -1] = '\0';
            finished = SUCCESS;
      
         }
    
         tmpPrice[10] = atof(tmpunitPricePtr);
         
         /***** stock level *****/
          do
      {
         printf("Stock Level: (1 - 100)\n");
         fgets(tmpstockLevelPtr, STOCKLEVEL_MAX + 2, stdin);
    
         /* fails if empty line is pressed*/
    
         if(tmpstockLevelPtr[0] == '\n')
         {
            printf("\t\n*****back to main menu*****");
            finished = FAILURE;
         }
    
    
         /* check the range of the stock */
    
         if(tmpstockLevelPtr[strlen(tmpstockLevelPtr) -1] != '\n')
         {
            printf(" Stock level needs to be between (0 -100)\n\n");
            readRestOfLine();
         }
    
         else
         {
            tmpstockLevelPtr[strlen(tmpstockLevelPtr) -1] = '\0';
            finished = SUCCESS;
      
         }
    
         tmpStock[STOCKLEVEL_MAX] = atoi(tmpstockLevelPtr);
         
         
    
      }while(finished == FAILURE);
    
    
         
    
      }while(finished == FAILURE);
     
    
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > tmpPrice[10] = atof(tmpunitPricePtr);
    This should be:
    tmpPrice = atof(tmpunitPricePtr);

    But instead of atof(), I believe strtod() is recommended, because I don't believe atof() is standard, or does any error checking.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    atof() is an ansi standard c function. One nice feature of strtod() is that you can pass it a big string that contains a mixture of numeric and non-numeric fields. strtod() will return a pointer to the character that stopped the scan, and your program can use it for other operations without having to rescan the whole string to find it.

    Of course the other nice feature of strtod() is that it will detect data overflow, which I don't think atof() does.
    Last edited by Ancient Dragon; 09-30-2005 at 08:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM