In this exercise from C++ Primer strings chapter, the directions read:
Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger. Now, change the program to report whether the strings have the same length, and if not, report which is longer.
To which I wrote:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
/* Write a program to read two strings and report whether the
strings are equal. If not, report which of the two is larger. Now, change
the program to report whether the strings have the same length, and if
not, report which is longer. */
int main()
{
string line;
string word;
getline(cin, line);
getline(cin, word);
if (line == word) {
cout << "String are equal." << endl;
}
else {
if (line > word)
cout << line << " is longer." << endl;
else {
cout << word << " is longer." << endl;
}
}
return 0;
}
Which seemed to work for the problem. Now for the first example of:
Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger.
I changed the comparisons to have .size(), and for this one:
Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger.
I removed the .size() for the comparisons. I printed out both size and .length() and I got the same answers, so I was wondering if I am misinterpreting this problem or was it to show that length and size are really the same thing?