Thread: newbie problem

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    newbie problem

    I have a newbie-ish problem (not that I'm a newbie )

    I'm using the strcmp function for like the 1st time and i get this error:

    Code:
    error C2664: 'strcmp' : cannot convert parameter 2 from 'char' to 'const char *'
    I'm using the following variables in the following call to strcmp():
    Code:
    char bo = 'b';
    char must_be_str = bo;
    char entered[MAX_PATH];
    
    if(strcmp(entered, must_be_str) == 0) {
    // whatever
    }
    Its probably becuase the first is declared as a single char and the other is declared as an array of chars of size MAX_PATH (defined somewhere else). The question is: what do I do to dodge this obstacle? I've tried this:
    Code:
    char bo[MAX_PATH];
    bo[0] = 'b';
    char must_be_str = bo;
    char entered[MAX_PATH];
    
    if(strcmp(entered, must_be_str) == 0) {
    // whatever
    }
    but when I do this, its still doesn't work. Any ideas? BTW, the 'entered' variabled is taken from a TextField using GetDlgItemText.

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    The " == " operator isn't overloaded for char's like it is for 'strings' explaining your dilemma with

    Code:
    if(strcmp(entered, must_be_str) == 0)
    I would consider converting your char data to strings as a quick (?) fix if you want to use 'strcmp'.

    (Your reference to a "TextField" brings Java to mind and Java text fields contain strings.)

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    i think the problem is

    char must_be_str = bo;

    this should be

    char must_be_str[3]="bo";
    -

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    bo is the name of the variable which contains 'b'

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    how do I get around this const char* bussiness when I only use chars? Why did they make the fuction only use const char*s?

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    strcmp probably doesn't check single chars for comparision.

    it checks only arrays

    also you try strcpy() to copy bo to must_be_str and then see...
    -

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I don't see the problem. You are trying to compare a character, so don't use the strcmp function.
    Code:
    char bo = 'b';
    char entered[MAX_PATH];
    
    if(*entered == bo) {
    // whatever
    }

  8. #8
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Yeah Monster is right.
    For arrays: strcmp()
    For single chars: use the == operator

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 PM
  3. Newbie ... looping problem
    By StupidIntel in forum C++ Programming
    Replies: 12
    Last Post: 05-13-2004, 06:45 PM
  4. Problem with code (newbie question)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2002, 01:39 AM
  5. newbie coding problem
    By rippascal in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2002, 11:45 PM