简体   繁体   中英

C++ - Error E2285 : Could not find a match for 'tolower(char *)' in function parseInput(fstream &)

Given the following code:

 void parseInput(fstream &inputFile) {
        const int LENGTH = 81;
        char line[LENGTH];

        while(!inputFile.fail()) {
            inputFile.getline(line,LENGTH);
            line = tolower(line);
            cout << line << endl;
        }
    }

upon compiling I'm getting this error:

Error E2285 : Could not find a match for 'tolower(char *)' in function parseInput(fstream &)

I know it returns an int, but not an int[], does that mean instead of using getline i should get input character to character? is there a way to convert the whole line to lower? Thanks in advance for everyone's help!

Hi tolower function input parametr must be char not char*, but if you use std you can use string and std:transform to make string lower case

std::string data = “MyStrData”; 
std::transform(data.begin(), data.end(), data.begin(), ::tolower);

The standalone tolower function only accepts one int , and the int needs to be strictly nonnegative or EOF , otherwise behavior is undefined. Another version of tolower exists, which however is a template. Both of these facts make it difficult to use them with transform easily and safely.

C++ also provides tolower in its ctype facet, which you can use here

std::ctype<char> const& c = std::use_facet< std::ctype<char> >(std::locale());
c.tolower(line, line + std::strlen(line));

However the whole code shows you aren't familiar with arrays and points, so maybe you should start using std::string and easy to use algorithms? Look into boost::string_algo 's case conversions .

Hm, three answers here managed to use tolower incorrectly .

Its argument must be non-negative or the special EOF value, otherwise Undefined Behavior. If all you have are ASCII characters then the codes will all be non-negative, so in that special case, it can be used directly. But if there's any non-ASCII character, like in Norwegian "blåbærsyltetøy" (blueberry jam), then those codes are most likely negative, so casting the argument to unsigned char type is necessary.

Also, for this case, the C locale should be set to the relevant locale.

Eg, you can set it to the user's default locale, which is denoted by an empty string as argument to setlocale .

Example:

#include <iostream>
#include <string>           // std::string
#include <ctype.h>          // ::tolower
#include <locale.h>         // ::setlocale
#include <stddef.h>         // ::ptrdiff_t

typedef unsigned char   UChar;
typedef ptrdiff_t       Size;
typedef Size            Index;

char toLowerCase( char c )
{
    return char( ::tolower( UChar( c ) ) );     // Cast to unsigned important.
}

std::string toLowerCase( std::string const& s )
{
    using namespace std;
    Size const      n   = s.length();
    std::string     result( n, '\0' );

    for( Index i = 0;  i < n;  ++i )
    {
        result[i] = toLowerCase( s[i] );
    }
    return result;
}

int main()
{
    using namespace std;
    setlocale( LC_ALL, "" );                    // Setting locale important.
    cout << toLowerCase( "SARAH CONNER LIKES BLÅBÆRSYLTETØY" ) << endl;
}

Example of instead doing this using std::transform :

#include <iostream>
#include <algorithm>        // std::transform
#include <functional>       // std::ptr_fun
#include <string>           // std::string
#include <ctype.h>          // ::tolower
#include <locale.h>         // ::setlocale
#include <stddef.h>         // ::ptrdiff_t

typedef unsigned char   UChar;

char toLowerCase( char c )
{
    return char( ::tolower( UChar( c ) ) );     // Cast to unsigned important.
}

std::string toLowerCase( std::string const& s )
{
    using namespace std;
    string          result( s.length(), '\0' );

    transform( s.begin(), s.end(), result.begin(), ptr_fun<char>( toLowerCase ) );
    return result;
}

int main()
{
    using namespace std;
    setlocale( LC_ALL, "" );                    // Setting locale important.
    cout << toLowerCase( "SARAH CONNER LIKES BLÅBÆRSYLTETØY" ) << endl;
}

For an example of using the C++ level locale stuff instead of C locale, see Johannes' answer.

Cheers & hth.,

You can simply perform the following loop to convert a string, character-by-character, to lower case:

const int lineLen = strlen( line );
for ( int i = 0; i < lineLen; i++ )
{
     line[i] = static_cast< char >( ::tolower( static_cast< unsigned char >( line[i] ) ) );
}

Edit: furthermore the following code is even easier (assuming the string is null terminated).

_strlwr( line );

yes and no. you can get a whole line but print it char after char (and using tolower) using a for loop.

tolower() only work on one character at a time. You should do something like

for(int i = 0; i < LENGTH; ++i)
  line[i] = tolower(line[i]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM