I new to programming and am having troubles writing C++ programs and would really appreciate help with the following question.

Write a C++ program that reads from keyboard 3 words, with proper input prompt. Then for these 3 words that were read, the program displays first the word longest length, then the word of the shortest length and finally the remaining last word. You may assume that these 3 words are all different in lengths, or consider a word to be of the longest length if no other words exceed it in length.

what part(s) of that do you know how to write? Break the problem down into smaller parts and it will be a lot easier for you to write. For example: to start off "Write a c++ program". To that much first by just writing the shell of main(). "reads from the keyboard" should tell you that you need to do that using cin from <iostream> header file. Only after you have that part working do you want to do some more of the rest. Write a little then compile and fix all errors/warnings. Then repeat.

this is my attempt. but i used getline

#include<iostream>
#include<string>
using namespace std;

int main()
{
int l1,l2,l3;
string message1, message2, message3;

cout << "Enter first string:";
getline(cin,message1);

cout << "Enter second string:";
getline(cin,message2);

cout << "Enter third string:";
getline(cin,message3);

l1=message1.length();
l2=message2.length();
l3=message3.length();

if (l1 > l2 && l3)
    
if ((l1 > l2) && (l1 > l3))
    
 cout << message1;
    
 else if ((l3 < l2) && (l2 < l1))
    
 cout << message3;
    
  else if ((l2 > l3) && (l2 < l1))
    
  cout << message2;
    
  system("pause");
   
    return 0;

}

i don't understand how am i to use cin instead of getline and if i use getline the program doesn't execute.

since your only typing in one word you could have used

cin>> message1;

but it doesn't really matter
Your if statements are wrong try some nested if statements for example

if(len1 > len2 && len1 > len3)
{
    cout<< message1 << endl;

    if(len2 > len3 )
    {
        cout<< message2 << endl << message3<< endl;
    }

    else
    {
         cout<< message3 << endl << message2<< endl; 
    }
}

else if( etc)

then try the same for the other two messages

>>i don't understand how am i to use cin instead of getline
You are confused. The getline() that you posted does use cin. Look closely at line 14. Your question/comment make me wonder if you even wrote that code.

>>i don't understand how am i to use cin instead of getline
You are confused. The getline() that you posted does use cin. Look closely at line 14. Your question/comment make me wonder if you even wrote that code.

I did write this code. i'm confused because when i showed this to my tutor he told me that i should use cin instead of getline.

What your tutor said was the same that that _Nestor said in his post. Re-read his post to see how to do that.

Thank you _Nestor and Ancient dragon for helping. The program executes perfectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.