CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    18

    Question compare the char & substr()

    I compare the chars:
    char cInLine[i][30];
    ...
    if((cInLine[i][2]==":")&&(cInLine[i][5]==":")&&(cInLine[i][11]==";")&&(cInLine[i][14]==":")&&(cInLine[i][17]==":"))
    { .....},
    but, I is told that "'==' : no conversion from 'char *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    e:\My Study\My VC++\temp for caption of movie\caption\captionDlg.cpp(260) : error C2040: '==' : 'int' differs in levels of indirection from 'char [2]' ",

    by the way, how can I get some chars from a string of cInLine[i]?

    thanks!

  2. #2
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: compare the char & substr()

    Change your string to be ':' not ":"

    cInLine[i][2]==':'

    ":" is a null terminated string and therefore is 2 bytes long and you can't compare that to a single character. ':' on the other hand IS a single character.
    Requests such as
    "I need to write an new language compiler by next week, I have teach yourself c++ in 21 days, can someone give me example code?" will be ignored.

  3. #3
    Join Date
    Apr 2005
    Posts
    18

    Re: compare the char & substr()

    Thanks!

    How about the second question?

  4. #4
    Join Date
    May 2004
    Posts
    109

    Re: compare the char & substr()

    Quote Originally Posted by xmasry
    how can I get some chars from a string of cInLine[i]?
    do you mean how you can get a character from one of the strings in cInLine?

    to get character 5 from string 2, do something like:

    Code:
    char MyChar = cInLine[5][2];

  5. #5
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: compare the char & substr()

    Hello xmasry,

    If you want a character (at position m, say) from cInLine[n], you can use cInLine[n][m]. If you want a group of characters (say p characters starting from position m) as string, you may have to do like this:

    Code:
    
    char* cPart = new char[p + 1];
    if (cPart)
    {
    	// Copies string and terminates with null
    	memmove(cPart, cInLine[n] + m, p);
    	cPart[p] = 0;
    	// Use string cPart
    	...
    	// Delete memory allocated
    	delete[] cPart;
    }
    
    A word of advice: Use std::string instead of char*. It is easier.

    Regards.
    Pravin.
    28-04-2005.
    Last edited by Pravin Kumar; April 28th, 2005 at 12:05 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured