#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;
}

Recommended Answers

All 13 Replies

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.

if ( strcmp( s1, s2, 5 ) > 0 )  //5, for example

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.

First, allocate sufficient memory for your strings. For example:

char s1[128] = ""; //exact size depends on how big an input you expect
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.

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...

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.

commented: You're long enough on this forum to know that you should post using code tags! -2

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:

    1. or, you change it to: cin.getline(s1,9,'\n');
    2. or you change char s1[10]; to char s1[11];

Same applies for s2 :)

To tux:http://www.cplusplus.com/reference/iostream/istream/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 :)

Well the objective is just to compare two strings right? Then even though strcmp can do the work wouldn't the usage as

#include<string>
using namespace std;
//within main
string str1 str2;
//inputs to strings as cin<<string
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?

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?

No, at least not for the purpose of his assignment. He is probably learning about character arrays, not c++ STL classes.

No, at least not for the purpose of his assignment. He is probably learning about character arrays, not c++ STL classes.

May be you are right Ancient Dragon (you always are :))but he has just concentrated his attention on strings only and never mentioned strings as character array so I thought giving this information would be fruitful and Dani is also about providing better ways right?

There is nothing wrong with trying to provide better ways to do something, as long as it meets the OPs purpose. If I'm trying to learn French there is no use speaking to me in Chinese.

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:

    1. or, you change it to: cin.getline(s1,9,'\n');
    2. or you change char s1[10]; to char s1[11];

Same applies for s2 :)

O thanks dear. I know the NULL character but I forgot it.
Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.