help with an if statement

Hello everyone. I'm quite new to the c++ program so i'm assuming my problem is relatively simple:). I've been given a question asking me to input three words into a program and then have it display the length of each word from largest to smallest. I've had a go and played around for hours but still cant get the program to run. If anyone could take a look and help it'd be greatly appreciated!


#include <iostream>

using namespace std;
int main()
{
string word1, word2, word3;
cout<<"Please enter a word:";
cin>>word1;
cout<<"Please enter a second word:";
cin>>word2;
cout<<"Please enter a third word:";
cin>>word3;


if (word1.size()>word2.size()>word3.size())

cout<<word1<<" has a length of: " << word1.size();
cout<<word2<<" has a length of: " << word2.size();
cout<<word3<<" has a length of: " << word3.size();

else if (word1.size() > word3.size() > word2.size())

cout<<word1<<" has a length of: " << word1.size();
cout<<word3<<" has a length of: " << word3.size();
cout<<word2<<" has a length of: " << word2.size();

else if (word2.size() > word1.size() > word3.size())

cout<<word2<<" has a length of: " << word2.size();
cout<<word1<<" has a length of: " << word1.size();
cout<<word3<<" has a length of: " << word3.size();

else if (word2.size() > word3.size() > word1.size())

cout<<word2<<" has a length of: " << word2.size();
cout<<word3<<" has a length of: " << word3.size();
cout<<word1<<" has a length of: " << word1.size();

else if (word3.size() > word1.size() > word2.size())

cout<<word3<<" has a length of: " << word3.size();
cout<<word1<<" has a length of: " << word1.size();
cout<<word2<<" has a length of: " << word2.size();

else if (word3.size() > word2.size() > word1.size())

cout<<word3<<" has a length of: " << word3.size();
cout<<word2<<" has a length of: " << word2.size();
cout<<word1<<" has a length of: " << word1.size();

system("pause");
return 0;

}
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;
int main()
{
string word1, word2, word3;
cout<<"Please enter a word:";
cin>>word1;
cout<<"Please enter a second word:";
cin>>word2;
cout<<"Please enter a third word:";
cin>>word3;


/*If word1 is larger or equals word2. AND(&&) if word2 is larger or equals word3. Cout the order.*/
if (word1.size() >= word2.size() && word2.size() >= word3.size())
{
 cout<<word1<<" has a length of: " << word1.size()<<endl;
 cout<<word2<<" has a length of: " << word2.size()<<endl;
 cout<<word3<<" has a length of: " << word3.size()<<endl;
}

system("pause");
return 0;

}


Use this for every combination you can think of.

Greetings Rope.

P.S. Didn't have the time to write a method for it. Though that would be possible to do.
Thanks! I never even considered writing the circumstances like that lol. Saved me:D
Using vectors and the STL sorting algorithm would be a far superior way of solving the problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

#define NUMBER_OF_WORDS 3

struct length {
	bool operator() ( const string& a, const string& b )
	{
		return a.size() > b.size();
	}
};

int main()
{
	vector<string> words(NUMBER_OF_WORDS);

	cout << "Please enter " << NUMBER_OF_WORDS << " words, one at a time." << endl;
	for (int i = 0; i < NUMBER_OF_WORDS; i++)
		cin >> words[i];

	sort(words.begin(), words.end(), length());
	system("CLS");

	cout << "The lengths of the words are the following." << endl;
	for (int i = 0; i < NUMBER_OF_WORDS; i++)
		cout << words[i] << ": " << words[i].length() << " characters long." << endl;
	return 0;
}


The code is tested and works like a charm.
Last edited on
Alternatively, you could do something like the following, but that brings problems when wanting to display the word next to it's number of characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

#define NUMBER_OF_WORDS 3

int main()
{
	vector<string> words(NUMBER_OF_WORDS);
	vector<size_t> sizes(NUMBER_OF_WORDS);

	cout << "Please enter " << NUMBER_OF_WORDS << " words, one at a time." << endl;
	for (int i = 0; i < NUMBER_OF_WORDS; i++)
	{
		cin >> words[i];
		sizes[i] = words[i].length();
	}

	sort(sizes.begin(), sizes.end());
	reverse(sizes.begin(), sizes.end());
	system("CLS");

	cout << "The lengths of the words are the following." << endl;
	for (int i = 0; i < NUMBER_OF_WORDS; i++)
		cout << sizes[i] << endl;
	return 0;
}
Topic archived. No new replies allowed.