View Single Post
Old Jun 6th, 2007, 6:10 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Find the longest word

I've got this other program which I came up with on my own, and I'm not sure if I'm even going about it the right way, cuz I really haven't studied arrays that much yet. The program has three words in an array of strings and uses a loop to find out which word was the longest:

int main()
{
	string words[] = {"Bank", "Office", "Mississippi"};
    string max = words[0];
	
	for (int i = 0; i< 3; i++)

		if (words[i] > max)
	      max = words[i];

		  cout << "The longest word was " << max;

		  cout << endl;

	return 0;
}

But when I run the program it says the longest word is "Office", when the longest word is really "Mississippi."

So I don't know if there is something wrong with my loop like maybe it's only executing two times and stopping at "Office" or what.

Like I said, I'm not even sure I'm going about this the right way.
357mag is offline   Reply With Quote