Click here to Skip to main content
15,887,896 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: C++ Thousand Separators Pin
awesometrickycool1-Apr-10 15:08
awesometrickycool1-Apr-10 15:08 
QuestionRe: C++ Thousand Separators Pin
David Crow1-Apr-10 15:33
David Crow1-Apr-10 15:33 
AnswerRe: C++ Thousand Separators Pin
awesometrickycool1-Apr-10 15:49
awesometrickycool1-Apr-10 15:49 
GeneralRe: C++ Thousand Separators Pin
David Crow1-Apr-10 16:26
David Crow1-Apr-10 16:26 
GeneralRe: C++ Thousand Separators Pin
Joe Woodbury1-Apr-10 16:02
professionalJoe Woodbury1-Apr-10 16:02 
GeneralRe: C++ Thousand Separators Pin
awesometrickycool1-Apr-10 16:17
awesometrickycool1-Apr-10 16:17 
GeneralRe: C++ Thousand Separators Pin
David Crow1-Apr-10 16:29
David Crow1-Apr-10 16:29 
GeneralRe: C++ Thousand Separators Pin
Joe Woodbury1-Apr-10 17:05
professionalJoe Woodbury1-Apr-10 17:05 
Starting point off top of my head:

(I emphasize STARTING. I wrote this in less than ten minutes, it probably doesn't compile or even work, but is similar to code I've written. There are a myriad of ways to do this. For error reporting, you could use an index instead of incrementing the string [i.e. pStr[offset] ]

The main point is that it's just plain code, nothing fancy. No tricks. And will be very fast.)

inline
bool IsNumber(TCHAR ch)
{ return ch >= '0' && ch <= '9'; }

bool ParseVal(LPCTSTR& pStr, float& val)
{
  val = 0.0f;

  bool isNegative = false;

  if (*pStr == '+')
  {
    pStr++;
  }
  else if (*pStr == '-')
  {
    pStr++;
    isNegative = true;
  }

  if (!IsNumber(*pStr))
    return false;

  bool firstSegment = true;
  int count = 0;
  while (*pStr)
  {
    TCHAR ch = *pStr++;
    // you could use a switch, but it wouldn't be much faster and breaking
    // out of the while loop is a pain
    if (ch == 'E' || ch == ' ')
    {
      break;
    }
    else if (ch == ',' || ch == '.') // should check for localized value [you could cache a static value)
    {
      if (firstSegment)
      {
        firstSegment = false;

        if (count > 3)
          return false;
      }
      else
      {
        if (count != 3)
          return false;
      }
      count == 0;
    }
    else if (IsNumber(ch))
    {
      if (++count > 3)
        return false;

      // you could also start calculating the value here
    }
    else
    {
      return false;
    }
  }
  return true;
}

GeneralRe: C++ Thousand Separators Pin
awesometrickycool3-Apr-10 12:48
awesometrickycool3-Apr-10 12:48 
GeneralRe: C++ Thousand Separators Pin
awesometrickycool3-Apr-10 12:49
awesometrickycool3-Apr-10 12:49 
GeneralRe: C++ Thousand Separators Pin
Joe Woodbury3-Apr-10 13:04
professionalJoe Woodbury3-Apr-10 13:04 
GeneralRe: C++ Thousand Separators Pin
awesometrickycool3-Apr-10 13:30
awesometrickycool3-Apr-10 13:30 
GeneralRe: C++ Thousand Separators Pin
Joe Woodbury3-Apr-10 15:06
professionalJoe Woodbury3-Apr-10 15:06 
QuestionProblem with glut when linking statically Pin
jocasa1-Apr-10 2:25
jocasa1-Apr-10 2:25 
QuestionRe: Problem with glut when linking statically Pin
CPallini1-Apr-10 2:41
mveCPallini1-Apr-10 2:41 
AnswerRe: Problem with glut when linking statically Pin
jocasa1-Apr-10 2:51
jocasa1-Apr-10 2:51 
GeneralRe: Problem with glut when linking statically Pin
CPallini1-Apr-10 2:58
mveCPallini1-Apr-10 2:58 
GeneralRe: Problem with glut when linking statically Pin
jocasa1-Apr-10 3:00
jocasa1-Apr-10 3:00 
GeneralRe: Problem with glut when linking statically Pin
jocasa1-Apr-10 3:22
jocasa1-Apr-10 3:22 
GeneralRe: Problem with glut when linking statically Pin
LunaticFringe1-Apr-10 3:43
LunaticFringe1-Apr-10 3:43 
GeneralRe: Problem with glut when linking statically Pin
jocasa1-Apr-10 4:12
jocasa1-Apr-10 4:12 
GeneralRe: Problem with glut when linking statically Pin
LunaticFringe1-Apr-10 4:24
LunaticFringe1-Apr-10 4:24 
GeneralRe: Problem with glut when linking statically Pin
CPallini1-Apr-10 6:16
mveCPallini1-Apr-10 6:16 
GeneralRe: Problem with glut when linking statically Pin
Moak1-Apr-10 6:26
Moak1-Apr-10 6:26 
GeneralRe: Problem with glut when linking statically Pin
LunaticFringe1-Apr-10 8:11
LunaticFringe1-Apr-10 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.