tolower error

This is a discussion on tolower error within the C++ Programming forums, part of the General Programming Boards category; Bonjour guys! I'm not very familiar to the function tolower() and when i tried it, i get some kind of ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    tolower error

    Bonjour guys!

    I'm not very familiar to the function tolower() and when i tried it, i get some kind of error.

    Here's some of my code:
    Code:
    #include <iostream>
    #include <cctype>
    
    
        string swedish[0];
        swedish[0] = "hej";
        swedish[1] = "vem";
        
        //Alla våra engelska ord i en array
        string english[0];
        english[0] = "hi";
        english[0] = "who";
        
        //Kör en while-sats för att alla ord ska vara med
        while (done < quantity) 
        {
              
              string lower = tolower(english[done]); //Gör alla bokstäver små
              
              if (insert = lower) //kollar om det är rätt
              {
              } 
        done++;
        }
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    #1. The declarations of the english and swedish arrays need to indicate at least 2 elements, i.e.
    Code:
    string swedish[2];
    swedish[0] = "hej";
    swedish[1] = "vem";
        
    //Alla v&#229;ra engelska ord i en array
    string english[2];
    english[0] = "hi";
    english[1] = "who";
    #2.
    Code:
    if (insert = lower) //kollar om det &#228;r r&#228;tt
    I'm sure you meant:
    Code:
    if (insert == lower) //kollar om det &#228;r r&#228;tt
    #3.
    Code:
    string lower = tolower(english[done]); //G&#246;r alla bokst&#228;ver sm&#229;
    tolower returns an int, not a char or a string. It also accepts an int as an argument, not a string. This function works on a character-by-character basis meaning you would have to apply it in a loop of some sort. The int argument and return value is the representation of the lowered character value.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Don't write the lowercasing code in-line. Write it once as a function, then use it multiple times. This function converts strings to lowercase:

    Code:
    #include <string>
    #include <iterator>
    #include <cctype>
    
    std::string string_lower(const std::string &str)
    {
       std::string x;
       std::transform(str.begin(), str.end(), std::back_inserter(x), tolower);
       return x;
    }

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Gets an error about the transform()

    Code:
    Code:
    #include <iostream>
    #include <cctype>
    #include <iterator>
    
    using namespace std;
    
    string string_lower(const string &str)
    {
       string x;
       transform(str.begin(), str.end(), back_inserter(x), tolower);
       return x;
    }
    
    int main(int argc, char *argv[])
    {
        //Alla v&#229;ra svenska ord i en array
        string swedish[2];
        swedish[0] = "hej";
        swedish[1] = "vem";
        
        //Alla v&#229;ra engelska ord i en array
        string english[2];
        english[0] = "hi";
        english[1] = "who";
        
        /*Alla ord ska ligga i ordning s&#229; att swedish[2] representerar det svenska ordet f&#246;r english[2]*/
        //Precis XD
        
        string insert; //Namnet p&#229; stringen som anv&#228;ndaren skriver in i
        int quantity = 3; //Hur m&#229;nga ord det finns + 1. Om det finns 2 fr&#229;gor ska det vara 3 h&#228;r. Om det &#228;r 4 fr&#229;gor ska det vara 5 h&#228;r.
        int done = 0; //Hur m&#229;nga ord som har blivit svarade
        int points = 0; //Hur m&#229;nga po&#228;ng man f&#229;r
        
        //Skriver ut en text
        cout << "H&#228;r kommer du f&#229; svenska ord som du ska &#246;vers&#228;tta!\nD&#229; b&#246;rjar vi!\n\n\n";
        
        //K&#246;r en while-sats f&#246;r att alla ord ska vara med
        while (done < quantity) 
        {
              cout << "Vad heter " << swedish[done] << " p&#229; engelska?\n"; //Skriver ut vad heter lala beronde p&#229; vilket ord det &#228;r
              cin >> insert; //L&#229;ter anv&#228;ndaren skriva in ett ord
              
              string lower = string_lower(swedish[done]); //G&#246;r alla bokst&#228;ver sm&#229;
              
              if (english[done] == lower) //kollar om det &#228;r r&#228;tt
              {
                         points = points + 1; //L&#228;gg till 1 po&#228;ng
              } 
        done++;
        }
        
        cout << "Du fick " << points << " po&#228;ng!";
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That's because transform() is in <algorithm>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by dwks View Post
    That's because transform() is in <algorithm>.
    Whoopsie. I hate how standard headers include each other willy-nilly. On my system the #include <algorithm> isn't needed (I think <iterator> pulls it in) so I didn't catch that.

    Nice save.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Same error:
    11 C:\Users\Fredrik\Desktop\main.cpp no matching function for call to `transform(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::back_insert_iterator<std::string>, <unknown type>)'

    Why is that?

    EDIT:

    Got Dev-C++ if that mathers
    Last edited by Livijn; 05-30-2007 at 03:26 PM.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Livijn View Post
    Same error:
    11 C:\Users\Fredrik\Desktop\main.cpp no matching function for call to `transform(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::back_insert_iterator<std::string>, <unknown type>)'

    Why is that?
    The clue is that <unknown type> thing at the end. It apparently doesn't know what tolower is. This is problematic because on many systems, tolower() is defined as a macro instead of a function. On my system, the <cctype> header undefines these macros and replaces them with true functions. Apparently your system does not. So... Try wrapping it up like this:

    Code:
    class lower
    {
    public:
        char operator()(char ch) { return tolower(ch); }
    };
    And then replace the "tolower" in the transform call with "lower()" (note the parentheses) -- does that work?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's because of tolower(), which should be std::tolower(). [edit] You can use tolower() if you include <ctype.h>, but <cctype> puts everything in the std namespace as far as I know. [/edit]

    You don't have to use transform(). Here's another way to do it.
    Code:
    #include <string>
    #include <iterator>
    #include <cctype>
    
    std::string string_lower(const std::string &str) {
        std::string x;
        for(std::string::iterator i = str.begin(); i != str.end(); ++ i) {
            x += std::tolower(*i);
        }
    
        return x;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Now i get another error

    17 C:\Users\Fredrik\Desktop\main.cpp expected primary-expression before ')' token

    Code:
    #include <iostream>
    #include <cctype>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    string string_lower(const string &str)
    {
           class lower
           {
                 public:
                        char operator()(char ch) { return tolower(ch); }
           };
    
       string x;
       transform(str.begin(), str.end(), back_inserter(x), lower);
       return x;
    }

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by dwks View Post
    It's because of tolower(), which should be std::tolower().

    You don't have to use transform(). Here's another way to do it.
    That's just a hand-coded transform()... Why do that when you already have it? And yeah, std::tolower is probably the right way.

    I caught you in mid-edit when you said you suspected back_inserter to be the problem... back_inserter DOES return an output iterator -- this is the whole purpose of it.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Livijn View Post
    Now i get another error
    I TOLD you not to forget the parentheses on lower()

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    But then i get the same error as earlier with tolower()

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Livijn View Post
    But then i get the same error as earlier with tolower()
    Are you sure it's the same? Can you post it?

  15. #15
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    17 C:\Users\Fredrik\Desktop\main.cpp no matching function for call to `transform(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::back_insert_iterator<std::string>, string_lower(const std::string&)::lower)'

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21