| | |
Problem Converting String to Float - Using ATOF
![]() |
•
•
Join Date: Nov 2008
Posts: 9
Reputation:
Solved Threads: 0
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:
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!
C Syntax (Toggle Plain Text)
char *price_1, *price_2; double price1, price2; if(strncmp(t, stock_id, 5) == 0) { printf("Found the stock in %s!\n", argv[1]); // printf ("Splitting string \"%s\" into tokens:\n", t); out = strtok(t," "); int i = 0; while (i < 1) { printf ("%s\n", out); out = strtok(NULL, " "); price_1 = out; i++; } //printf("Printing price_1: %s\n", price_1); while (i < 2) { out = strtok(NULL, " "); price_2 = out; i++; } printf("Printing price_1: %s\n", price_1); price1 = atof(price_1); printf("Printing price1: %d\n", price1); break; }
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!
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Dir creating, permissions setting issue!
- Next Thread: need help!!!
| Thread Tools | Search this Thread |
* adobe api array asterisks binarysearch calculate centimeter changingto char character cm copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory feet fgets file floatingpointvalidation forloop frequency function givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o infiniteloop input interest intmain() iso kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list looping loopinsideloop. lowest match meter microsoft mqqueue mysql number oddnumber odf open openwebfoundation pdf posix power probleminc process programming pyramidusingturboccodes radix read recv recvblocked repetition research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard string suggestions threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





