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

Recommended Answers

All 20 Replies

I've seen this identical problem just a day or so ago.

Nice you posted your homework problem, now what is it that you expect from us? I don't have my crystle ball at the moment so I can't read your mind.

please try to tell me do i use if statement and how

post what you have done so far to solve this problem.

#include<iostream>
using namespace std;
int main( ) 

{
  char word, word1, word2,word3, length;
  cout<< "Enter three word:";
  cin>> word; 
  if (word = 'word1')
  cout<<"the length is more than 5";
  else if (word = 'word2')
  cout<<" the length is equal to five";
  else if  (word = 'word3')
  cout<<"the length is less than five";
  else
  cout<<"invalid word"; 
  system("pause");
  return 0 ;
}

and didnt work out

line 9. When comparing two strings you have to use the == boolean operator for equality, not the = assignment operator. And the literal string must be in double, not single quotes if( word == "word1") line 10: The comparison on line 9 does not check for word length so the comment on line 10 is meaningless. On line 9 you probably need to check for the length of the string instead of comparing two strings. Like this: if( word.length() > 5) You have to change lines 11 and 13 similar to above.

#include<iostream>
using namespace std;
int main( ) 

{
  char word, word1, word2,word3, length;
  cout<< "Enter three word:";
  cin>> word; 
  if ( word.length() > 5)
  cout<<"word1";
  else if ( word.length() = 5)
  cout<<" word2";
  else if ( word.length() < 5)
   cout<<"word3";
  else
  cout<<"invalid word"; 
  system("pause");
  return 0 ;
}

i did this and didnt work out so whatis wrong with it

char word, word1, word2,word3, length;

You've declared everything as chars, not strings. A char is ONLY ONE character, not a word.
So declare them as strings like this: std::string word1, word2,word3; Length doesn't have to be declared

and you still have one if statement wrong. Please re-read AD's post

Niek

line 1: you have to include <string> header file in order to use std::string class.

line 11: you still have the wrong operator -- should be ==, not =. Remember that = is an assignment operator while == is boolean.

Still wrong

Let me look into my Crystal Ball and see what's wrong...

[...]

;)

Please post your code as it is now, and post any error-messages

Let me look into my Crystal Ball and see what's wrong...

Ohh so now I know where I left mine :)

Darn, I knew I read that quote somewhere, but it was in this very thread.. Sorry for being a copy-cat :P

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

int main() {
	const int number_of_words = 3;
	string words[number_of_words];
	for (int i = 0; i < number_of_words; ++i) {
		cout << "Enter Word Number " << i+1 << endl;
		cin >> words[i];
		cin.ignore(255, '\n');
	}
	bool isChange = true;
	while (isChange) {
		isChange = false;
		for (int i = 0; i < number_of_words-1; ++i) {
			if (words[i].size() < words[i+1].size()) {
				words[i].swap(words[i+1]);
				isChange = true;
			}
		}
	}
	cout << words[0] << ' ' << words[2] << ' ' << words[1] << endl;
	return 0;
}

fix 10x to niek_e

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

I assume Your program suppose to be accepting three words from the key board but ur only accepting one word or is the program comparing the characters.... coz as I analyze your code here the user is gona enter one word which is word and compare it with word1 , word2 and word3 where are those coming from… what I think you should do is to ask the user to enter word1, word2 and word3 that means you gona have 3 cin ’s and compare those words using if statement

Darn, I knew I read that quote somewhere, but it was in this very thread.. Sorry for being a copy-cat :P

No problem -- some people expects us to be psychics. :)

for (int i = 0; i < number_of_words; ++i) {
      if (words[i].size() < words[i+1].size()) {

So what happens if 'i' becomes 2? Then in the second line you try to acces words[3], but it doesn't exist.... --> Undefined behaviour or Crash.

Niek

unti now u didnt solve my problem and its due during 2 hours so please show me the easy way to finish

you idiot, i give you already working code
see my last post

it didnt work and i need to used if statement and more simple

*sigh*

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

int main() {
	const int number_of_words = 3;
	string words[number_of_words];
	for (int i = 0; i < number_of_words; ++i) {
		cout << "Enter Word Number " << i+1 << endl;
		cin >> words[i];
		cin.ignore(255, '\n');
	}

	if (words[0].size() > words[1].size() && words[0].size() > words[2].size()){
		cout << words[0] << ' ';
	}
	else if (words[1].size() > words[0].size() && words[1].size() > words[2].size()){
		cout << words[1] << ' ';
	}
	else
		cout << words[2] << ' ';
	if (words[0].size() < words[1].size() && words[0].size() < words[2].size()){
		cout << words[0] << ' ';
	}
	else if (words[1].size() < words[0].size() && words[1].size() < words[2].size()){
		cout << words[1] << ' ';
	}
	else
		cout << words[2] << ' ';
	if (words[1].size() > words[0].size() && words[0].size() > words[2].size()){
		cout << words[0] << ' ';
	}
	else if (words[0].size() > words[1].size() && words[1].size() > words[2].size()){
		cout << words[1] << ' ';
	}
	else if (words[1].size() < words[0].size() && words[0].size() < words[2].size()){
		cout << words[0] << ' ';
	}
	else if (words[0].size() < words[1].size() && words[1].size() < words[2].size()){
		cout << words[1] << ' ';
	}
	else
		cout << words[2] << ' ';
	
	return 0;
}
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.