• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

cannot get strcmp c++ to work correctly char array


Open in new window

Hello i'm trying to learn arrays and i think i have the hang of it a little but i'm trying to compare two strings to see if they are equal and it's not working.. my program ask the user for the firstname, middle last name.
If the first name and last name are the same should display "first name and last name are the same"
 if the firstname and middle name are the same should display "firstname and middle name are the same"
if firstname , middle name and last name are all the same it should display  "firstname, lastname and middle are the same" but i'm screwing up somewhere.. and it always displays "firestname and last name are the same" even though i know they are not the same.

#include <iostream>
#include <cstring>
using namespace std;

int main(void)
{

 char first_name[256];
 char last_name[256];
 char middle_name[256];
 
 


 cout << "\nEnter your first name: ";
 cin.getline(first_name,256);
 cout <<endl;

 cout <<"enter your middle name: ";
 cin.getline(middle_name,256);

 cout <<"\nenter your last name: ";
 cin.getline(last_name,256);

 cout <<endl;

  int total = strlen(first_name)+strlen(middle_name)+strlen(last_name);

 cout <<"your full name is: " ;

 strcat(first_name, " ");
cout <<strcat(first_name,middle_name);


cout <<" " <<last_name;

cout <<endl;

cout <<"total number of charaters in your name is "; 

cout <<total<<endl;


if(strcmp(first_name,last_name))
{
	cout <<"your first name and last  are the same";

}


}

Open in new window

0
businessesatoz
Asked:
businessesatoz
4 Solutions
 
1ceCommented:
>>if(strcmp(first_name,last_name))

change to
if(strcmp(first_name,last_name) == 0)
0
 
ZoppoCommented:
Hi businessesatoz,

the problem is that the
 strcat(first_name, " ");
cout <<strcat(first_name,middle_name);

Open in new window

adds a space and the middle name to 'first_name' so if 'first_name' and 'last_name' where identical before they differ after the 'strcat'.

You can simply skip the 'strcat' calls and just put out the name like this:
cout <<"your full name is: " ;
cout << first_name << " " << middle_name << " " << last_name;

Open in new window

Hope that helps,

ZOPPO
0
 
xeltekCommented:
HI, strcmp will return 0, that is false in boolean, when the two strings are equal.
You just change the line 44 to:
if(!strcmp(first_name,last_name))

Open in new window


that will work.

Here is the reference:
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

Return Value
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Pramod BugudaiCommented:
Hi. It seems you don't have any idea of using strcmp() function. Your comparison for checking first name and last name doing nothing. see the attached code, how to use strcmp().


let me know for any further doubts.
#include <string.h>
#include <stdio.h>

char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";

int main( void )
{
   char tmp[20];
   int result;
   /* Case sensitive */
   printf( "Compare strings:\n   %s\n   %s\n\n", string1, string2 );
   result = strcmp( string1, string2 );
   if( result > 0 )
      strcpy( tmp, "greater than" );
   else if( result < 0 )
      strcpy( tmp, "less than" );
   else
      strcpy( tmp, "equal to" );
   printf( "   strcmp:   String 1 is %s string 2\n", tmp );
   /* Case insensitive (could use equivalent _stricmp) */
   result = _stricmp( string1, string2 );
   if( result > 0 )
      strcpy( tmp, "greater than" );
   else if( result < 0 )
      strcpy( tmp, "less than" );
   else
      strcpy( tmp, "equal to" );
   printf( "   _stricmp:  String 1 is %s string 2\n", tmp );
}

Open in new window

0
 
sarabandeCommented:
i strongly recommend against using !strcmp though it was technically correct.

the questioner expected a boolean return from strcmp what is a widely occuring error cause strcmp serves as substitute for == operation which is a boolean expression. one should not encourage the wrong interpretation of the return value of strcmp as a boolean by using the ! operator. that makes code badly readable and is highly error-prone because of that.

Sara
0
 
businessesatozAuthor Commented:
i'm not sure why the heck it's still not working.. i want to use strcmp in my code.

#include <iostream>
#include <cstring>
using namespace std;

int main(void)
{

 char first_name[256];
 char last_name[256];
 char middle_name[256];
 
 int result;


 cout << "\nEnter your first name: ";
 cin.getline(first_name,256);
 cout <<endl;

 cout <<"enter your middle name: ";
 cin.getline(middle_name,256);

 cout <<"\nenter your last name: ";
 cin.getline(last_name,256);

 cout <<endl;

  int total = strlen(first_name)+strlen(middle_name)+strlen(last_name);

  result = (strcmp(first_name,last_name));

 cout <<"your full name is: " ;

 strcat(first_name, " ");
cout <<strcat(first_name,middle_name);


cout <<" " <<last_name;

cout <<endl;

cout <<"total number of charaters in your name is "; 

cout <<total<<endl;

if(result > 0)
{
	cout <<"first name and last name is the same";

}
}

Open in new window

0
 
businessesatozAuthor Commented:
nevermind please ignore my last comment.. changing result ==0 got it work.

if(result == 0)
{
	cout <<"first name and last name is the same";

}
}

Open in new window

0
 
businessesatozAuthor Commented:
thanks again for all your help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now