0

So basically I have a string array called QandA[2][55].

QandA[0][0-55] contains questions (0-55 is the range)

QandA[1][0-55] contains the answer to these questions

Now I have to make a loop that will transfer all the strings from QandA[1][1-55] to QandA[2][1-55] in the same order but lowercase. I have tried this code but it doesn't seem to work. I've also checked that the string arrays work by using a loop to cout all the strings from 0-55. My code that has an error is shown below.

for (int i = 0; i < 55; i++) {
    QandA[2][i] = tolower(QandA[1][i]);
}

This gives me an error saying "No suitable conversion from std::string to int exists" which I don't understand because I assumed tolower was for string values, not int.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523

1 Answers1

1

Although your code will likely have other issues (eg, out of bounds access in the first dimension of QandA), the problem you are seeing here is that std::tolower is supposed to take a single character:

char lowerChar = std::tolower('C');

If you want to make a whole string lower case you need to do it per character in the string. There are plenty of ways to do it, for eg. How to convert std::string to lower case?

Taking from this answer, your code code be:

for (int i = 0; i < 55; i++) 
{
    QandA[2][i] = std::transform(QandA[1][i].begin(), 
                                 QandA[1][i].end(), 
                                 QandA[1][i].begin(), 
                                 ::tolower);
}

Also remember that to accomplish this the declaration of QandA must be:

std::string QandA[3][55];

although if you can use c++11, prefer:

std::array<std::array<std::string, 55>, 3> QandA;

for more features and better everything.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175