Comparing char[]

Hello! I want to compare alphabetically two arrays. I try this:

1
2
3
4
char a[10] = "AABC";
char b[10] = "ZABC";
if(a > b) cout << a;
else cout << b;


But it always outputs a. How can I compare char arrays?
Oh I got it. If I use strcmp(a, b); the function returns 1 if a is bigger than b, 0 if they are same and -1 if b is bigger than a :)
As a proficient C++ programmer, you should be using std::string instead of char arrays, and std::vector or smart pointers instead of non-text arrays.

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/string/string/operators/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
    std::string a("AABC");
    std::string b = "ZABC"; // traditional style, if you wish

    if (a > b)
        std::cout << a;
    else
        std::cout << b;

    std::cout << std::endl;
}
Thanks for the suggestion. I was wondering how it is possible with char arrays :)
Topic archived. No new replies allowed.