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

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; } } 

when compiling, I get this error:

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

I know that it returns int, but not int [], does this mean that instead of using getline, I have to get the input character for the character? is there any way to convert the whole string to the bottom? Thank you in advance for your help!

+6
c ++ compiler-errors tolower
source share
6 answers

You can simply execute the following loop to convert the string, character by character, to lowercase:

 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: in addition, the following code is even simpler (assuming the line is null terminated).

 _strlwr( line ); 
0
source share

The input parameter of the mylower function should be char not char *, but if you use std, you can use string and std: transform to make lowercase lowercase

 std::string data = "MyStrData"; std::transform(data.begin(), data.end(), data.begin(), ::tolower); 
+6
source share

The stand-alone tolower function accepts only one int , and the int must be strictly non-negative or EOF , otherwise the behavior is undefined. There is another version of tolower , which, however, is a template. Both of these facts make it difficult to use them with transform easily and safely.

C ++ also provides a tolower in the ctype factor, 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, all the code shows that you are not familiar with arrays and points, so maybe you should start using std::string and easy-to-use algorithms? boost::string_algo conversion cases .

+3
source share

Hm, the three answers here have been able to misuse the tolower .

Its argument must be a non-negative or special value of EOF , otherwise Undefined Behavior. If all you have is ASCII characters, then the codes will be non-negative, so in this special case it can be used directly. But if there is any character other than ASCII, as in the Norwegian "blåbærsyltetøy" (blueberry jam), then these codes are most likely negative, so you must use the unsigned argument char .

In addition, for this case, the locale C must be set to the appropriate locale.

For example, you can set it to the default user locale, which is indicated by an empty string as an 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; } 

An example of using this with 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 language standard instead of the C language standard, see Johannes answer.

Cheers and hth.,

+1
source share

Yes and no. you can get the whole string, but print its char after char (and using tolower) using a for loop.

0
source share

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

 for(int i = 0; i < LENGTH; ++i) line[i] = tolower(line[i]); 
-one
source share

All Articles