C++ toupper()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eboyjr14
    New Member
    • Apr 2007
    • 27

    C++ toupper()

    I am very new to C++. I am just learning and wanted to make a simple program that capitalizes the input. What is wrong with this? I use Bloodshed Dev-C++. I get this error:

    no matching function for call to `toupper(std::s tring&)'
    candidates are: int toupper(int)

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        while( true )
               {
               cout << "fire> ";
               string cmd;
               getline(cin, cmd);
               string upp;
               upp = toupper(cmd);
               cout << upp << endl << endl;
               }
        return EXIT_SUCCESS;
    }
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by eboyjr14
    I am very new to C++. I am just learning and wanted to make a simple program that capitalizes the input. What is wrong with this? I use Bloodshed Dev-C++. I get this error:

    no matching function for call to `toupper(std::s tring&)'
    candidates are: int toupper(int)

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        while( true )
               {
               cout << "fire> ";
               string cmd;
               getline(cin, cmd);
               string upp;
               upp = toupper(cmd);
               cout << upp << endl << endl;
               }
        return EXIT_SUCCESS;
    }

    use char * instead of string

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You don't need to revert to C and char*. You can still use your string. Each element of the string is a char.
      [code=cpp]
      string str("aBcDeF");
      int length = str.size();
      for (int i = 0; i < length; ++i)
      {
      str[i] = toupper(str[i]);
      }
      cout << str << endl;
      [/code]

      You can even avoid writing the loop byb using the transform() algorithm:
      [code=cpp]
      string str("aBcDeF");
      transform(str.b egin(), str.end(), str.begin(), toupper);
      cout << str << endl;
      [/code]

      Now you are down to three lines of code.

      Comment

      • bluesteel
        New Member
        • Mar 2007
        • 84

        #4
        Originally posted by eboyjr14
        I am very new to C++. I am just learning and wanted to make a simple program that capitalizes the input. What is wrong with this? I use Bloodshed Dev-C++. I get this error:

        no matching function for call to `toupper(std::s tring&)'
        candidates are: int toupper(int)

        Code:
        #include <cstdlib>
        #include <iostream>
        #include <string>
        
        using namespace std;
        
        int main(int argc, char *argv[])
        {
            while( true )
                   {
                   cout << "fire> ";
                   string cmd;
                   getline(cin, cmd);
                   string upp;
                   upp = toupper(cmd);
                   cout << upp << endl << endl;
                   }
            return EXIT_SUCCESS;
        }
        Be careful:
        use #include <cstring> or #include <string.h> but be careful because <string> does not exist!!

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by bluesteel
          Be careful:
          use #include <cstring> or #include <string.h> but be careful because <string> does not exist!!
          What does this mean?

          string.h is the C string header.
          cstring is the C++ version of the C header
          string is the header for the C++ string class inthe C++ Standard Library

          Why do you say <string> does not exist? It has to exist if the compiler is sold as C++ compiler.

          Comment

          • joeschnell
            New Member
            • Apr 2007
            • 47

            #6
            You would do well to remove the arguments in the main () when starting to use Dev, using their template causes you to follow those argument parameters. I would get rid of the template return exit success also, simply build a template with your most often used headers, int main () and a return 0; I like to add a system ("pause"); also prior to the return statement otherwise a lot of programs I write I cannot get to display in the cmd screen.

            A cstdlib does include # <string>, works for me anyway. When using the manipulator toupper I always include <iomanip>, don't know if you need the library or not but it seems easier to simply use it if your not sure. Anytime I am manipulating anything even setw(3) I use #include <iomanip>, give it a shot it may help you out. You also refer to your 'get' with 'cin' the common input operator which I believe you needed to call prior to, I'm just learning myself.
            Whenever I use any of the following I include iomanip in my file headers.

            C++ I/O Manipulators

            boolalpha
            dec
            fixed
            hex
            internal
            left
            noboolalpha
            noshowbase
            noshowpoint
            noshowpos
            noskipws
            nounitbuf
            nouppercase
            resetiosflags
            right
            scientific
            setbase
            setfill
            setiosflags
            setprecision
            setw
            showbase
            showpoint
            showpos
            skipws
            unitbuf
            uppercase
            ws

            Good luck.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by eboyjr14
              no matching function for call to `toupper(std::s tring&)'
              candidates are: int toupper(int)
              I thought I would just mention because, no-one else has, that the actual reason for this warning is that the only toupper() function that exists is in the C runtime library and has this prototype

              int toupper( int ch );

              you need to include ctype.h (C) or cctype (C++)

              This function takes a character value (in C character constants have type int hence the function takes and returns int) and returns the uppercased version of that character.

              You tried to pass a string to toupper, since that does not match this prototype your compiler looked for a function overload that did take a string, failed to find one and produced the error.

              Comment

              • garrywang57
                New Member
                • Apr 2019
                • 2

                #8
                What does this mean?

                string.h is the C string header.
                cstring is the C++ version of the C header
                string is the header for the C++ string class inthe C++ Standard Library

                Why do you say <string> does not exist? It has to exist if the compiler is sold as C++ compiler.
                Last edited by NeoPa; May 2 '19, 11:58 PM. Reason: Removed illegal link.

                Comment

                Working...