RSS Forums RSS

Problem Converting String to Float - Using ATOF

Please support our C advertiser: Programming Forums
Reply
Posts: 9
Reputation: Krysis is an unknown quantity at this point 
Solved Threads: 0
Krysis Krysis is offline Offline
Newbie Poster

Problem Converting String to Float - Using ATOF

  #1  
Nov 24th, 2008
Hello yet again... Anyway, I'm trying to convert a string that has the form of "1097.04", that is, decimal fixed point with two digits after the decimal point. My code currently reads this number from a file then stores it in a *char. Then when i try to convert this *char into a double it instead returns a 0. Here's my code:

  1. char *price_1, *price_2;
  2. double price1, price2;
  3.  
  4. if(strncmp(t, stock_id, 5) == 0)
  5. {
  6. printf("Found the stock in %s!\n", argv[1]);
  7. // printf ("Splitting string \"%s\" into tokens:\n", t);
  8. out = strtok(t," ");
  9.  
  10. int i = 0;
  11. while (i < 1)
  12. {
  13. printf ("%s\n", out);
  14. out = strtok(NULL, " ");
  15. price_1 = out;
  16. i++;
  17. }
  18. //printf("Printing price_1: %s\n", price_1);
  19. while (i < 2)
  20. {
  21. out = strtok(NULL, " ");
  22. price_2 = out;
  23. i++;
  24. }
  25. printf("Printing price_1: %s\n", price_1);
  26. price1 = atof(price_1);
  27. printf("Printing price1: %d\n", price1);
  28.  
  29. break;
  30. }

Where 't', is the buffer that reads from the file line-by-line and argv[1] is the file being read in, 'datafile', which is of form [Stock] [Price_1] [Price_2]:
IPHON 400.00 500.00
WALMA 50.00 75.00
KROGE 100.00 120.00
CHOOC 24.00 34.24
MICRO 1234.56 2000.00

Here's my output:

-bash-3.2$ ./StockOrders datafile logfile server client
Broker is waiting for request...
Request quote to buy MICRO by 5555.

Client says, "Request quote to buy MICRO by 5555.
."
Client wants to buy...
Printing stock_id: MICRO
Printing cust_id: 5555
Broker is generating quote...
Found the stock in datafile!
MICRO
Printing price_1: 1234.56
Printing price1: 0
-bash-3.2$


Thanks again!
AddThis Social Bookmark Button
Reply With Quote  
Posts: 524
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 78
Murtan Murtan is offline Offline
Posting Pro

Re: Problem Converting String to Float - Using ATOF

  #2  
Nov 25th, 2008
I'm thinking that your problem is actually the format character in the last printf. '%d' is for decimals/integers. Try using a '%f' instead.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C Forum
Views: 777 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:34 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC