#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 PostP.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.
Jump to PostThere 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.
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
tux4life commented: You're long enough on this forum to know that you should post using code tags! -2
tux4life 2,072 Postaholic
Dave Sinkula 2,398 long time no c Team Colleague
tux4life 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.