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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 3
Reputation: san gabriel is an unknown quantity at this point 
Solved Threads: 0
san gabriel san gabriel is offline Offline
Newbie Poster

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

 
0
  #1
Jun 3rd, 2009
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7.  
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10. char s1 = "";
  11. char s2 = "";
  12. cout << "Input String1: " ;
  13. cin >> *s1;
  14. cout << "Input String2: " ;
  15. cin >> *s2;
  16.  
  17. if ( strcmp( s1, s2 ) > 0 )
  18. cout << "\nThe first string is greater than the second string." << endl;
  19. else if ( strcmp( s1, s2 ) == 0 )
  20. cout << "\nThe first string is equal to the second string." << endl;
  21. else if ( strcmp( s1, s2 ) < 0 )
  22. cout << "\nThe first string is less than the second string." <<endl;
  23. return 0;
  24. }
Last edited by Tekmaven; Jun 4th, 2009 at 3:27 am. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

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

 
0
  #2
Jun 3rd, 2009
Your use of strcmp( ) is correct.

However, your strings aren't strings. You must allocate memory for s1 and s2 as arrays of char. Or declare them as char pointers and then allocate with the new operator.

Your subject says "strncmp( )". If you want to use that variant, which compares only up till a difference or that number of characters, whichever comes first, you must add a third argument, the number of characters limit.
  1. if ( strcmp( s1, s2, 5 ) > 0 ) //5, for example
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: san gabriel is an unknown quantity at this point 
Solved Threads: 0
san gabriel san gabriel is offline Offline
Newbie Poster

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

 
0
  #3
Jun 3rd, 2009
I don't know how to input 2 string ( like, s1: csu la ece department. and s2: csu la admissom.)
And I don't know what is the right way to fix this program.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

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

 
0
  #4
Jun 3rd, 2009
First, allocate sufficient memory for your strings. For example:
  1. char s1[128] = ""; //exact size depends on how big an input you expect
  2. char s2[128] = "";

Now, what you have will work, to a point. The use of cin >> s1; will limit your input to a single word. If you want to get in multi-word strings, you need to use the getline( ) method.

I'll leave it as an exercise for the reader to look up the proper usage of that - it's been amply explained here on DaniWeb, and everywhere else.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #5
Jun 4th, 2009
Besides that you don't understand a role of stdafx.h in Visual C++. Place all standard header includes in stdafx.h (not in main module)!

Read about pre-compiled headers in Visual Studio help...
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: mirfan00 has a little shameless behaviour in the past 
Solved Threads: 2
mirfan00 mirfan00 is offline Offline
Light Poster

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

 
-1
  #6
Jun 4th, 2009
Dear you are declaring a char ( char s1 = "" ). Its not a string fis=rst declare a string as
char s1[10]; // string s1 of 10 characters
char s2[10]; //string s2 of 10 characters
Now take input as
cin.getline(s1,10,'\n');
cin.getlin(s2,10,'\n');
Now compare two strings. It will compile and work.
I hope you enjoy it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

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

 
0
  #7
Jun 4th, 2009
Originally Posted by mirfan00 View Post
Dear you are declaring a char ( char s1 = "" ). Its not a string fis=rst declare a string as
char s1[10]; // string s1 of 10 characters
char s2[10]; //string s2 of 10 characters
Now take input as
cin.getline(s1,10,'\n');
cin.getlin(s2,10,'\n');
Now compare two strings. It will compile and work.
I hope you enjoy it.
First some remarks:
  1. Explicitly specifying the delimiter in cin.getline(s1,10,'\n'); isn't necessary.
  2. char s1[10]; // string s1 of 10 characters
    You aren't taking the NULL-terminator '\0' into account.
  3. cin.getline(s1,10,'\n'); : if you want this line to behave correctly then you should do one of the following:
    • or, you change it to: cin.getline(s1,9,'\n');
    • or you change char s1[10]; to char s1[11];
Same applies for s2
Last edited by tux4life; Jun 4th, 2009 at 3:13 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
1
  #8
Jun 4th, 2009
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

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

 
0
  #9
Jun 4th, 2009
To tux:http://www.cplusplus.com/reference/i...tream/getline/
"Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation"

EDIT Dave:Umm, Guess you were faster. Dani is loading real slow. I am thus using a console based browser. Sorry couldn't see you
Last edited by siddhant3s; Jun 4th, 2009 at 7:11 pm.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

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

 
0
  #10
Jun 4th, 2009
Well the objective is just to compare two strings right? Then even though strcmp can do the work wouldn't the usage as
  1. #include<string>
  2. using namespace std;
  3. //within main
  4. string str1 str2;
  5. //inputs to strings as cin<<string
  6. if(str1==str2)//comparision
Wouldn't the above implementation be more efficient? As we are using standard namespace.

>San you can go through this.

P.S: Guys the person "san gabriel" doesn't know about string classes I suppose may be new to c++ so wouldn't the new info be better?
Last edited by csurfer; Jun 4th, 2009 at 11:12 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 597 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC