strcmp() error when compiling need help

This is a discussion on strcmp() error when compiling need help within the C++ Programming forums, part of the General Programming Boards category; I am just trying to compile a simple string compare program and it will not work. I am using g++ ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    strcmp() error when compiling need help

    I am just trying to compile a simple string compare program and it will not work. I am using g++ and here is the error


    strcomp.cpp: In function `int main (...)':
    strcomp.cpp:10: no matching function for call to `basic_string<char,
    string_char_traits<char>, __default_alloc_template<true, 0> >::strcmp
    (char *&, const char[3])'

    And here is the code:

    Code:
    #include <iostream>
    #include <string>
    
    void main()
    {
            char *vari;
            cout << "Type up: ";
            cin >> vari;
            string stri;
            if(stri.strcmp(vari,"up")==0)
            {
                    cout << "\nGood\n";
            }
            else
            {
                    cout << "\nBad\n";
                    cout << "You entered " << vari << endl;
            }
            return 0;
    
    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Here's a little spiffing up I did.

    Code:
    #include <iostream>
    #include <string>
    
    int main()//should be int main
    {
            char *vari;
            cout << "Type up: ";
            cin >> vari;
            //string stri; commented out because not needed
            if(strcmp(vari,"up")==0)//removed stri. because not correct
            {
                    cout << "\nGood\n";
            }
            else
            {
                    cout << "\nBad\n";
                    cout << "You entered " << vari << endl;
            }
            return 0;
    
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    2
    That worked thanks

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    char *vari;
    cout << "Type up: ";
    cin >> vari;



    This will crash sooner or later. You need to reserve actual space, not only a pointer to that space. Make vari an array of char that's large enough to hold the sentence.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21