If statements and conditionals.

Hello, I'm very much a newbie in C++; I'm following a course online and the current exercise is to make a console app that compares the length of two words and prints out which is the longer of the two.

Where I'm going wrong is with the if statements, when I compile (and run) the code after typing "cat" (3 chars) as the first word and "elephant" (8 characters) as the second, it proceeds to print out all the if statements as if it were true for all i.e. prints all three of them.

I feel like I'm just overlooking something incredibly simple but I've spent close to two hours just looking at the code but I'm failing to see where I'm going wrong.

All help is appreciated, thanks in advance!

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 #include <iostream>
#include <string>

using namespace std;

int main()
{
	string word1;
	string word2;

	cout << "Word length comparator." << endl;

	cout << "Please enter the first word." << endl;
	cin >> word1;
	cout << "You entered: " << word1 << endl;

	int l = word1.length();
		cout << "\"" + word1 + "\" is "
		<< l << " characters long." << endl;

	cout << "Please enter the second word." << endl;
	cin >> word2;
	cout << "You entered: " << word2 << endl;

	int l2 = word2.length();
		cout << "\"" + word2 + "\" is "
		<< l2 << " characters long." << endl;

		if (word1.length() ==  word2.length());
		{
			cout << "\"" + word1 + "\" with " << l << "characters, is the same length as" << "\"" + word2 + "\" with "
				<< l2 << " characters." << endl;
		}


		if (word1.length() > word2.length());
		{
			cout << "\"" + word1 + "\" with " << l << "characters, is greater than" << "\"" + word2 + "\" with "
				<< l2 << " characters." << endl;
		}

		if (word1.length() < word2.length());
		{
			cout << "\"" + word1 + "\" with " << l << "characters, is less than" << "\"" + word2 + "\" with "
				<< l2 << " characters." << endl;
		}


	return 0;
}
Last edited on
Check your semicolons.
Ahh thank you!

Oh man I feel so embarrassed. I knew it would be something so woefully simple that I overlooked.

Thanks again. :)
Debugging man. Debugging saves life.
Topic archived. No new replies allowed.