Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing the values of char arrays in C++

Tags:

c++

arrays

char

I have problem about doing something in my program. I have a char[28] array keeping names of people. I have another char[28] array that also keeps names. I ask user to enter a name for the first array, and second arrays reads names from a binary file. Then i compare them with == operator, But even though the names are the same, their values look different when i debug it. Why is this the case? How can i compare these two? My sample code is as follows:

int main()
{
    char sName[28];
    cin>>sName;      //Get the name of the student to be searched

      /// Reading the tables

    ifstream in("students.bin", ios::in | ios::binary);

    student Student; //This is a struct

    while (in.read((char*) &Student, sizeof(student)))
    {
    if(sName==Student.name)//Student.name is also a char[28]
    {
                cout<<"found"<<endl;
        break;
    }
}
like image 318
yrazlik Avatar asked Feb 24 '13 10:02

yrazlik


People also ask

How do you compare values in a char array?

You can compare char arrays that are supposed to be strings by using the c style strcmp function. In C++ you normally don't work with arrays directly. Use the std::string class instead of character arrays and your comparison with == will work as expected.

How do you compare char in C?

Use strcmp() to compare strings. The return value is 0 if strings are the same. The return value from strcmp() also indicates order: the value is < 0 or > 0 to indicate which string is lesser or greater.

How do you compare char data types?

Based on the Unicode table, the char primitive data type also has the associated integer value. Using ==, <, > operators you should be able to compare two characters just like you compare two integers. Note: Comparing char primitive values using < , > or == operators returns a boolean value.

How do I compare two arrays of arrays?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.


2 Answers

You can compare char arrays that are supposed to be strings by using the c style strcmp function.

if( strcmp(sName,Student.name) == 0 ) // strings are equal

In C++ you normally don't work with arrays directly. Use the std::string class instead of character arrays and your comparison with == will work as expected.

like image 73
nvoigt Avatar answered Sep 30 '22 06:09

nvoigt


Assuming student::name is a char array or a pointer to char, the following expression

sName==Student.name

compares pointers to char, after decaying sName from char[28] to char*.

Given that you want to compare the strings container in these arrays, a simple option is to read the names into std::string and use bool operator==:

#include <string> // for std::string

std::string sName;
....

if (sName==Student.name)//Student.name is also an std::string

This will work for names of any length, and saves you the trouble of dealing with arrays.

like image 45
juanchopanza Avatar answered Sep 30 '22 05:09

juanchopanza