# atoi convert problem

• 02-06-2003
Ray Schmidt
atoi convert problem
I thought I had this figured earlier, but I can't seem to break the FFF-II-SS (Feet-Inches-Sixteenths) out into a variable that I can convert into a decimal. "m_feet" holds the initial value given by a user say 241012 which equals 24.89583 (24'-10-3/4"). No matter what I change the m_feet2 =, I still get the origional keyed in input (241012). I'm not sure if I'm just missing something here (prob. am) but it seems to me that adjusting the formula at the end should provide me with different results. Could this have anything to do with this code being in an MFC App, because I get better results in this code in a console app.

Any help is greatly appreciated...

UpdateData(TRUE);

char string[8] = {0,0,0,0,0,0,0,0}; // +1 for nul
int six=0;
int inch=0;
int feet=0;

sprintf(string, "%f", m_feet);

six = atoi(&string[strlen(string) - 2]);
string[strlen(string) - 2] = '\0';
inch = atoi(&string[strlen(string) - 2]);
string[strlen(string) - 2] = '\0';
feet = atoi(string);

m_feet2 = ((((six / 16.0) + inch) / 12.0) + feet); // prob. here!!!

UpdateData(FALSE);
• 02-07-2003
Sebastiani
if you are getting a number from the end there, keep in mind too that strlen()-2 is a great place for one of the carriages return codes to be lurking. Try strlen()-4 and strlen()-3 instead.
• 02-07-2003
Ray Schmidt
no difference
Even doing this, I retain the same output. Surely moving the pointer back to -3 or -4 should have given me anything different than the exact same results as -2. Is there a better function to try instead of using atoi, maybe using a pointer on an intiger instead of converting it (is that possiable)? I open on using any type of solution/suggestions....

Thanks....
Ray Schmidt
• 02-07-2003
Cela
>>sprintf(string, "%f", m_feet);
Your code assumes that m_feet is an integer with no fractional part. If m_feet is an int then no print will take place and string will have nothing but null characters. If m_feet is a double or float then the sprintf will succeed, but when you try to break it up with atoi it'll mess up because it has a fractional part. The fix is very simple, change the call to sprintf to
Code:

`sprintf(string, "%.0f", m_feet);`
:-)
• 02-07-2003
Hammer
Not a particularly safe effort, but this might get you further.
Code:

```#include <iostream> #include <stdio.h> using std::cout; using std::endl; int main(void) {   char input[] = "241012";   int feet;   int inches;   int sixteenths;   float total;     sscanf (input, "%2d%2d%2d", &feet, &inches, &sixteenths);     total = feet + (inches/12.0) + ((sixteenths/16.0)/12);     cout <<"feet:" <<feet       <<" inches:" <<inches       <<" sixteenths:" <<sixteenths       <<" total:" << total;   return 0; }```
You'd need to add quite a bit of error checking to make it safe ;)
• 02-07-2003
Ray Schmidt
Thank You!!!
Man,

You all are the greatest. If this message board were only avaiable 15 years ago, I might have learned c++ by now...

Thank you again....
Ray Schmidt
• 02-07-2003
CodeMonkey
GOD BLESS THE METRIC SYSTEM!