0

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.

3
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by Race
0

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.

0

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.

0

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

0

>>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.

0

>>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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.