0

I was just doing a simple coding challenge for c++ as I am relatively new to the language, I wanted to compare two strings to each other lexicographically, so as to print out 1 if the first string entered was lexicographically bigger than the second one, and to print out -1 if the opposite was true. As I was looping, I wanted to only compare them equally in lower case, so if a character from the first string was a capital "A" I wanted to turn it into a lower case "a", and that same rule applied to every character for both strings that were being compared. When I tried to implement that idea in my if statement using the <cctype> header like so... first_str[i].tolower() and the same for the second string, I got the error "expression must have class type". Here is my code:

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main() {

    int first{};
    int second{};

    string first_str{};
    string second_str{};

    cin >> first_str;
    cin >> second_str;


    for (size_t i{}; i < first_str.length(); ++i) {
        // The if statement below has one accessing the character by indexing with the square brackets
        // and the other one has it through the .at() method provided by the <string> header
        if (first_str[i].tolower() > second_str.at(i).tolower()) {
            ++first;
        } else if (first_str.at(i).tolower() < second_str.at(i).tolower()) {
            ++second;
        } else { // If they are equal then just add the count to both
            ++first;
            ++second;
        }
    }
    if (first > second)
        cout << 1 << endl;
    else if (first > second)
        cout << -1 << endl;
    else
        cout << 0 << endl;

    return 0;
}

I decided to investigate a little further but to no avail, like I said I am a beginner in C++, when I came across people with a similar compilation error, the answers would talk about things called pointers...that I haven't learnt yet and so was very confused. I want to know if I need to learn that to understand why this is happening or is there another reason to why this problem is occurring in my code. I will try my best to learn about pointers soon enough, and thank you for reading my problem.

  • There is not `tolower` method for `char` and there cannot be one because it is not a class type as the error says. – Quimby Oct 16 '20 at 08:55
  • @Quimby I am still a bit confused, I come from a language like python where there is a class for things like a string which also includes stand alone characters, and so assumed that char has it's own class type, I'm sorry. Does that mean that types like: int, char, and string literals don't actually have a class? – KrapnixTheFootballer Oct 16 '20 at 08:58
  • No, they do not, `char` is just one byte long. But there is a difference between string literal and `std::string`. If you are serious with the language you might want to look at the [list of good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Quimby Oct 16 '20 at 09:00
  • 1
    To add to the confusion: `char` and `int` are not classes, but `std::string` is. However a string literal does not have the type `std::string`, but rather `const char[]` – perivesta Oct 16 '20 at 09:03
  • @Quimby yep I ordered one and it's on its way home, I know what you are thinking, "bro, why didn't you just order the one that you can read online or download". I am just ermmm, yeah I have no explanation... – KrapnixTheFootballer Oct 16 '20 at 09:03
  • 1
    @dave I think I get it now actually, cause for c type string literals you have to create them like so: ´char my_string[5] {'H', 'e', 'l', 'l', 'o', \0};´ last space contains the null character right? – KrapnixTheFootballer Oct 16 '20 at 09:07
  • Almost, the size is 6 (the terminating \0 counts too). This works but is the more explicit syntax. If you write `const char my_string[] = "Hello";` it does exactly the same. – perivesta Oct 16 '20 at 09:09
  • @dave oh alright thank you for explaining! – KrapnixTheFootballer Oct 16 '20 at 09:10
  • @KrapnixTheFootballer I did a typo (forgot the variable name). Also note the \0 at the end is not written in the code, it is implicitly created at the end. – perivesta Oct 16 '20 at 09:13
  • @dave yep no worries, thank you, it really means a lot to someone who is starting out for people like you who just explain little things here and there. – KrapnixTheFootballer Oct 16 '20 at 09:15

1 Answers1

1

It's because first_str[i] is a char. Char doesn't have a .tolower() method.

So instead of:

first_str[i].tolower()

you should use:

tolower(first_str[i])

Przemysław Niemiec
  • 1,704
  • 1
  • 11
  • 14