comparing two words

closed account (16MoizwU)
I have another assignment in which it asks for two words and then compare them to write the first one and then the other. I think I have solved the first one but it's doesn't show the second word. the program that I write is

#include<iostream>
using namespace std;

int main() {
// prints two words and compare them
string x, y;
// user type the words taken by x and y respectively
cout << " x = " << x << " and y = " << y << endl;
cin >> x >> y;
// comapre two words
if(x < y) cout << " the first word is: " << x << endl;
else cout << " the second word is: " << y << endl;
return 0;
}

Please help me out.

Let my guys give you the whole question

Create a C++ program that accepts two words adn compare them. The output should say " the frist word is: " then the first word in alphabetic order should appear, and in other line " the second word is: " then the second word typed by the user.
Last edited on
How would you like to compare the two words? By alphabet, number of characters, length? O_o
Seeing that it is only showing the first word, apparently x is always less than y. But how can a string be less than something?

Do you mean how many letters, if they are the same word, which letters, what?
Yea, the way C++ will technically "compare" strings, like if i was asking which one was greater, it will look at the strings memory address. Your best bet will be to compare their size.
closed account (16MoizwU)
Let my guys give you the whole question

Create a C++ program that accepts two words adn compare them. The output should say " the frist word is: " then the first word in alphabetic order should appear, and in other line " the second word is: " then the second word typed by the user.
Try using a string compare function that you write yourself
closed account (16MoizwU)
yes i know i write down the compare function but it's only shows only one value which less but i have to have both values such this way- lower value showing on one line than the higher value showing in another line
If it's just two words. You can do something like this. Real simple. But further on down the line you might want to do something else that is more portable than if-else statement. hehe. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
string alpha, beta, gamma; 
cin >> alpha; 
cin >> beta; 
    
if(beta < alpha)
	{
		gamma = alpha;
		alpha = beta; 
		beta = gamma; 
	}

cout << alpha << " " << beta; 
}//main
Topic archived. No new replies allowed.