businessesatoz
asked on
cannot get strcmp c++ to work correctly char array
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";
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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";
}
}
ASKER
nevermind please ignore my last comment.. changing result ==0 got it work.
if(result == 0)
{
cout <<"first name and last name is the same";
}
}
ASKER
thanks again for all your help
You just change the line 44 to:
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