I'm to write a small program in which I input 3 words and display the three words from longest to shortest. I was wondering what keyword (which is what I believe they are called) does this. I don't plan on asking you to make said program, just to let me know what keyword needs to be used to do this.
A noob question?
Started by MPax, Mar 30 2008 03:43 PM
6 replies to this topic
#1
Posted 30 March 2008 - 03:43 PM
|
|
|
#2
Posted 30 March 2008 - 05:05 PM
I think you may want to use strlen to determine the length of each string.
#3
Posted 01 April 2008 - 07:57 PM
I'm not sure if strlen was working for me so I went and read my textbook and it told me to use string.length() and i've been playing around with it for a little.
I used the header file #include <string> and declared the variables as string (at least I believe I did). I then began using if else to determine how my information would be displayed. At first I was writing:
else if else if (word1.length() > word3.length() > word2.length())
cout << word1 << word3 << word2;
because that is how I understood the book. But I am fairly sure this is incorrect. Can someone please tell me how to do this properly?
I used the header file #include <string> and declared the variables as string (at least I believe I did). I then began using if else to determine how my information would be displayed. At first I was writing:
else if else if (word1.length() > word3.length() > word2.length())
cout << word1 << word3 << word2;
because that is how I understood the book. But I am fairly sure this is incorrect. Can someone please tell me how to do this properly?
#4
Posted 02 April 2008 - 07:51 AM
Well, at least now we know you're talking about C++ and not C.
With regard to the condition, you need to separate it a bit.
else if else if (word1.length() > word3.length() > word2.length())I'm hoping the 'else if else if' is just a typo.
With regard to the condition, you need to separate it a bit.
if (word1.length() > word3.length() [COLOR="Blue"]&& word3.length() >[/COLOR] word2.length() )
#5
Posted 02 April 2008 - 07:51 AM
You're right that your statement is incorrect, since the first > evaluates to 0 or 1, which probably isn't greater than word2.length().
Try:
else if (word1.length() > word3.length() && word3.length() > word2.length())
Try:
else if (word1.length() > word3.length() && word3.length() > word2.length())
#6
Posted 02 April 2008 - 06:14 PM
WingedPanther said:
You're right that your statement is incorrect, since the first > evaluates to 0 or 1, which probably isn't greater than word2.length().
Try:
else if (word1.length() > word3.length() && word3.length() > word2.length())
Try:
else if (word1.length() > word3.length() && word3.length() > word2.length())
Thank you that worked perfectly. I appreciate the help.
#7
Posted 03 April 2008 - 07:27 AM
You're welcome. It's a common issue when transitioning from the world of math where we write i<j<k to the world of programming where we have to decode that into i<j and j<k.


Sign In
Create Account

Back to top









