Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Dear Fellows .. !!
I write a code for the comparison of two character strings, but the problem really is its OUTPUT, after getting the string from user the program just finished having no output ...
please have an eye .. I'm keenly waiting your response . !!!!

What is the correct output of the function strcmp.. !! Also how can i write this please give me an example to illustrate at least i give a try.... P L E A S E . !!

C++
#include "stdafx.h"
#include <iostream>
#include <cstring>

using namespace std;

int strcmp(char str[], char str2[])
{
		if (str < str2)
		
			return -1;
		/*{
			cout << "l e s s e r" << endl;
		}*/
		else if (str > str2)
		
			return 1;
		/*{
			cout << "g r e a t e r" << endl;
		}*/
		else if (str == str2)
		
			return 0;
		/*{
			cout << "e q u a l i t y" << endl;
		}*/
                      // what should i write here.. ?? this is causing a warning message from compiler.

}

int main()
{ 
	const int length = 15;
	char red[length], black[length]; // two strings that can hold 15 characters including null character.
	cout << "Enter First STRING: ";
	cin.getline(red, length+1); //  user allowed to enter a string of 15 characters + 1 for null character. 
	cout << "Enter Second STRING: ";
	cin.getline(black, length+1);
	strcmp(red, black);
	system("Pause");
	return 0;

}
Posted
Updated 17-Mar-13 19:02pm
v2
Comments
Matthew Faithfull 17-Mar-13 16:53pm    
Which line would you be expecting to produce output from this after the string comparison?
Richard MacCutchan 18-Mar-13 6:11am    
There are so many things wrong with this code that you really need to step back and spend some time studying your C programming guides.

Besides that you forgot to output the result of you strcmp function, the function itself has a major problem. It compares the addresses of the two strings, not their contents. Note that when you refer to the name of an array that is the same as referring to the pointer to the first element.

strcmp should instead contain a loop that compares character by character the contents of the two strings.
 
Share this answer
 
v2
Comments
Espen Harlinn 17-Mar-13 18:29pm    
5'ed! It's kind of odd that nobody else noticed ...
nv3 18-Mar-13 5:15am    
Thanks, Espen!
Usman Hunjra 18-Mar-13 1:16am    
but how can i do that .. ?!
Usman Hunjra 18-Mar-13 1:28am    
o o p s .. i got it.. I've to call the strcmp function like this,,,
=> cout << strcmp(red, black);
Usman Hunjra 18-Mar-13 1:31am    
but know the problem is it will output 1 at every situation ... ?????????????
You should use std::string instead of C-style characters array. It is much easier to use and far less error prone including the fact that default comparison of std::string does something that is usually helpful.
 
Share this answer
 
It's not outputting anything because you've not told it to. You've used cout to output messages to prompt the user for input, but after calling strcmp, you only call "system("pause");".

As "bigcdh" has said, use the standard "strcmp" function, check the result and output something accordingly.

If you are going to implement strcmp yourself, you probably shouldn't output anything in it because you might want to use it in other situations. The places where you have commented out outputting statements, if you uncommented them as-is I would expect compiler errors. The last one, after the last return statement, I didn't know the C-compiler was that intelligent, but it is in fact unreachable code - if the first two if's are false, then the third one will by definition be true, so nothing in that function after the return will be executable.
 
Share this answer
 
 
Share this answer
 
v2
Comments
Espen Harlinn 17-Mar-13 18:28pm    
5'ed! That's what most of us would do ...
H.Brydon 19-Mar-13 14:23pm    
Right - I +5'd it too. :-)
bigcdh 19-Mar-13 16:10pm    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900