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

Discussion in 'C++' started by san gabriel, Jun 4, 2009.

  1. san gabriel

    san gabriel New Member

    Joined:
    Jun 4, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
    Last edited by a moderator: Jun 4, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > char s1 = "";
    > cin >> *s1;

    I doubt this will compile.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice