how to use strncmp function to compare two string input by user in C++

This is a discussion on how to use strncmp function to compare two string input by user in C++ within the C++ Programming forums, part of the General Programming Boards category; can you help me fix it Code: #include "stdafx.h" #include <iostream> #include <iomanip> #include <cstring> using namespace std; int _tmain(int ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Unhappy how to use strncmp function to compare two string input by user in C++

    can you help me fix it

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    using namespace std;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    char s1 = "";
    char s2 = "";
    cout << "Input String1: " ;
    cin >> *s1;
    cout << "Input String2: " ;
    cin >> *s2;
    
    if ( strcmp( s1, s2 ) > 0 )
    cout << "\nThe first string is greater than the second string." << endl;
    else if ( strcmp( s1, s2 ) == 0 )
    cout << "\nThe first string is equal to the second string." << endl;
    else if ( strcmp( s1, s2 ) < 0 )
    cout << "\nThe first string is less than the second string." <<endl;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    5
    dont you need to declare s1 & s2 as string variables or char arrays instead of a char?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    So do you know where do I need to fix?
    I am confused for this problem.
    Thank you.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    A couple of problems.

    1) You are declaring a char but assigning it a char *
    2) You are using said char as user input but you shouldn't do that.

    Solutions:
    1)Include <string>
    2)Change the char to a std::string
    3)Change strcmp to ==

    If you really need to output if a string is greater than another string, you can use strcmp but use the .c_str function of the string instance
    Code:
    std::string firstString = "first";
    std::string secondString = "second";
    int compareResult = strcmp(firstString.c_str(), secondString.c_str)
    //Your code here
    Last edited by prog-bman; 06-03-2009 at 09:20 PM.
    Woop?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Quote Originally Posted by prog-bman View Post
    If you really need to output if a string is greater than another string, you can use strcmp but use the .c_str function of the string instance
    Code:
    std::string firstString = "first";
    std::string secondString = "second";
    int compareResult = strcmp(firstString.c_str(), secondString.c_str)
    //Your code here
    There's no need to involve strcmp. strings have a less-than operator defined.

    The post title is inconsistent with the question here though. strncmp does something different to strcmp, that would take much more C++ code to replicate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Changing a input string to a function call?
    By cezaryn in forum C Programming
    Replies: 4
    Last Post: 07-16-2003, 02:37 PM

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