Thread: char parsing problem

  1. #1
    Unregistered
    Guest

    char parsing problem

    I'm trying to parse a char array and based on each individual letter value set an int value in a corresponding int array (it's for a bitmap font class in DirectX). Anyway here's what I've tried and where I'm stuck:

    Code:
    char *cTextIn, cCurLetter;
    strcpy(cTextIn,"ThisIsATest");
    int iLen=strlen(cTextIn);
    for(int i=0;i<iLen;i++)
    {
       cCurLetter=*cTextIn++;
       cout<<cCurLetter;
    }
    This works fine to give me the current letter incrimentally, but when I try to test that letter with strcmp I get an error during compile because it can't use a char where it needs a const char*.

    Code:
    int iPlaceInAlphabet;
    if(strcmp(cCurLetter,"a")==0) iPlaceInAlphabet=0;
    if(strcmp(cCurLetter,"b")==0) iPlaceInAlphabet=1;
    //etc. etc.
    To correct this I've tried using casting it which compiles fine but then I get an unhandled exception at runtime.

    Code:
    int iPlaceInAlphabet;
    if(strcmp((const char*)cCurLetter,"a")==0) iPlaceInAlphabet=0;
    if(strcmp((const char*)cCurLetter,"b")==0) iPlaceInAlphabet=1;
    //etc. etc.
    Does anyone else have experience with this?

    What I have is a bitmap with the letters of the alphabet in a row, each letter is 32x32 pixels. Based on each letter parsed it will set an int value in a corresponding int array. The values in this int array are used to determine the source RECTs for the letter's to be blitted.

    Thanks in advance for any help.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >char *cTextIn;
    >strcpy(cTextIn,"ThisIsATest");
    You can't do this, you haven't allocated any space, and cTextIn is pointing nowhere in particular. (Unless there's some additional code you just haven't shown here).

    >if(strcmp(cCurLetter,"a")==0) iPlaceInAlphabet=0;
    strcmp() compares strings, hence it's name. You are trying to compare a single character (cCurLetter), with a string ("a").

    If all you want to do is compare 2 characters, just do it directly:
    >if (cCurLetter == 'a')

    Also, if all you want is the character offset in the alphabet, you could do something like

    >iPlaceInAlphabet = cCurLetter - 'a';
    This assumes that cCurLetter is actually a letter [a-z] in the first place.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. C++ (char *) problem?
    By Teegtahn in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2008, 01:23 PM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM