Comparing the values of char arrays in C++

后端 未结 5 1932
广开言路
广开言路 2021-01-02 02:38

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

5条回答
  •  误落风尘
    2021-01-02 03:21

    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.

提交回复
热议问题