strcmp error

This is a discussion on strcmp error within the C++ Programming forums, part of the General Programming Boards category; my error is at the dark red line, i dont c whats wrong, im new at this as you can ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    strcmp error

    my error is at the dark red line, i dont c whats wrong, im new at this as you can c

    Code:
    #include <iostream>
    #include <sstream>
    #include <cstring>
    
    using namespace std;
    int main()
    {
    cout << "NAME: ";
    string a,b;
    int c;
    
    cin >> a;
    cout << "\n confirm name: ";
    
    cin >> b;
    c = strcmp(a,b);
    if (c == 0){
    cout << "\n \n confirmed.";}
    else{
    cout << "\n \n not confirmed";
    }
    }
    Check out my programming / algorithm blog @ http://www.swageroo.com

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    strcmp takes in two pointers to characters. If you want to use string compare then you need to do:
    Code:
    c = strcmp ( a.c_str(), b.c_str() );
    you could also use the built in compare function for std::string
    Code:
    if ( a == b )

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    In your program, you need to include <string>, and you should return something from main(), too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    int c++ the return 0; is implict

    While doing the include <string> is good form it is covered by the <sstream>

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    you should return something from main(), too.
    c++ does not require an explicit return value from main(), but I like to do it anyway just for clarity if nothing else.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This line declares two variables of type string:
    Code:
    string a,b;
    string types are defined in a header file called <string>, which also contains functions and operators you can use on strings, like ==. == compares two terms for equality. Since you are new, you will invariably try to use = to compare two terms, but that doesn't work. If you are comparing two terms, you have to use ==.

    There are also other kinds of strings called cstyle strings which are character arrays, e.g.

    char str[10] = "hello";

    There are functions you can use on character array strings and they are defined in a header file called <cstring>. When you include a file name, the contents of the file replaces the #include statement, which allows your program to use the definitions and functions in the header file. You can't use functions defined for character arrays on variables of type string, which is what you are trying to do.
    Last edited by 7stud; 11-25-2005 at 03:41 PM.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by Thantos
    strcmp takes in two pointers to characters. If you want to use string compare then you need to do:
    Code:
    c = strcmp ( a.c_str(), b.c_str() );
    you could also use the built in compare function for std::string
    Code:
    if ( a == b )
    yep thanks, i didnt know i could compare strings like that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

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