Strings and Relational Operators

I've been struggling with getting the correct output from a list of strings on an assignment I am doing. I created some sample code so it is short and perhaps simpler to therefore see the problem. The code below gives the incorrect max value, it says that "john" is greater than "hello" and outputs "john" in the cout line. What am I doing wrong?

#include <iostream>
#include <string>

using namespace std;

int main()
{
string array[4] = { "hello", "john", "bob", "carmine" };

string max = array[0];

for (int i = 1; i < 4; i++)
{
if (max < array[i])
max = array[i];
}

cout << max << endl;

return (0);
}
You are doing nothing wrong, you expect sth. wrong.
john is greater then hello because j comes after h in the alphabet.
Thank you so much! I've been pulling my hair out over this - been thinking that it was about the length of the string not the alphabetical order of it, great catch :)
Topic archived. No new replies allowed.