1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

need help cant evaluate a string???

Discussion in 'C++' started by Brett.h, Jan 29, 2010.

  1. Brett.h

    Brett.h New Member

    Joined:
    Dec 18, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I am writing a program that will evalute a string and check for spaces, when I try to compile it I get this error message:

    Error 1 error C2446: '==' : no conversion from 'const char *' to 'int' c:\documents and settings\brett\my documents\visual studio 2008\projects\project1\ai learn algorithm\ai learn algorithm\test.cpp 12

    what is wrong, and how can I fix it

    all help is appreciated,

    Brett


    getline(cin, teststr);
    int counter = 0;
    cout << teststr[counter];
    if (teststr[counter] == " "){
    cout << "test complete";
    }
     
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    Use single quotes for a single character. Double quotes designate an array of characters that decays to a pointer to type char when used as above. Try this:

    Code:
      if (teststr[counter] == ' '){
    
     
  3. murugaperumal

    murugaperumal New Member

    Joined:
    Feb 20, 2010
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    Dear Friend,

    To compare the two string ,instead of "==" operator use the compare function.

    Sample code
    Code:
    string str1 ("green apple");
    if (str1.compare(6,5,"apple") == 0)
    cout << "still, " << str1 << " is an apple\n";
    
     

Share This Page