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();
#include <iostream>
usingnamespace 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.
#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 {
booloperator() ( 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;
}
#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;
}