Jump to content

A noob question?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
MPax

MPax

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
I think you may want to use strlen to determine the length of each string.

#3
MPax

MPax

    Newbie

  • Members
  • Pip
  • 3 posts
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?

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Well, at least now we know you're talking about C++ and not C.
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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())
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
MPax

MPax

    Newbie

  • Members
  • Pip
  • 3 posts

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())

Thank you that worked perfectly. I appreciate the help.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog