Thread: atoi convert problem

  1. #1
    Registered User Ray Schmidt's Avatar
    Join Date
    Feb 2003

    Question 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...


    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!!!


  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    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.
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  3. #3
    Registered User Ray Schmidt's Avatar
    Join Date
    Feb 2003

    Question 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....

    Ray Schmidt

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    >>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
    sprintf(string, "%.0f", m_feet);

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Not a particularly safe effort, but this might get you further.
    #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
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User Ray Schmidt's Avatar
    Join Date
    Feb 2003

    Thumbs up Thank You!!!


    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

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-15-2009, 03:17 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. can atoi convert string to float number?
    By Jasonymk in forum C++ Programming
    Replies: 6
    Last Post: 03-14-2003, 04:40 AM