#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;
}
san gabriel
0
Newbie Poster
Recommended Answers
Jump to PostYour 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 …
Jump to PostFirst, 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 …
Jump to PostBesides 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...
All 13 Replies
vmanes
1,165
Posting Virtuoso
san gabriel
0
Newbie Poster
vmanes
1,165
Posting Virtuoso
ArkM
1,090
Postaholic
mirfan00
-19
Light Poster
mvmalderen
commented:
You're long enough on this forum to know that you should post using code tags!
-2
mvmalderen
2,072
Postaholic
Dave Sinkula
2,398
long time no c
Team Colleague
mvmalderen
commented:
Yes, you're right, I was wrong :$
+8
siddhant3s
1,429
Practically a Posting Shark
csurfer
422
Posting Pro
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
csurfer
422
Posting Pro
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
mirfan00
-19
Light Poster
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.