error trying to toupper string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sandy@murdocks.on.ca

    #1

    error trying to toupper string

    I am trying to upper case a string, so I have this method:

    string FileSystem::tou pper(string S)
    {
    for (int i=0; i<S.length(); ++i)
    {
    S[i]=toupper(S[i]);
    }

    return S;
    }

    I get the errors:
    invalid conversion from `char' to `const char*'

    `std::basic_str ing<_CharT, _Traits, _Alloc>::basic_ string(const
    _CharT*, const _Alloc&) [with _CharT = char, _Traits =
    std::char_trait s<char>, _Alloc = std::allocator< char>]'

    cannot convert `std::string' to `char' in assignment

    I did some looking on the Net and it appears I have done nothing new,
    in fact it is almost character by character a match of 3 samples I
    found on the net.

    But of course... it doesn't compile.

  • Jim Langston

    #2
    Re: error trying to toupper string

    <sandy@murdocks .on.cawrote in message
    news:1164766528 .879075.280170@ h54g2000cwb.goo glegroups.com.. .
    >I am trying to upper case a string, so I have this method:
    >
    string FileSystem::tou pper(string S)
    {
    for (int i=0; i<S.length(); ++i)
    {
    S[i]=toupper(S[i]);
    }
    >
    return S;
    }
    >
    I get the errors:
    invalid conversion from `char' to `const char*'
    >
    `std::basic_str ing<_CharT, _Traits, _Alloc>::basic_ string(const
    _CharT*, const _Alloc&) [with _CharT = char, _Traits =
    std::char_trait s<char>, _Alloc = std::allocator< char>]'
    >
    cannot convert `std::string' to `char' in assignment
    >
    I did some looking on the Net and it appears I have done nothing new,
    in fact it is almost character by character a match of 3 samples I
    found on the net.
    >
    But of course... it doesn't compile.
    This compiles and runs for me with the expected output of
    HELLO

    I'm using Micorost Visual C++ .net 2003

    #include <iostream>
    #include <string>

    std::string toupper(std::st ring S)
    {
    for (int i=0; i < S.length(); ++i)
    {
    S[i]=toupper(S[i]);
    }

    return S;
    }

    int main()
    {
    std::string MyString("Hello ");

    std::cout << toupper( MyString ) << "\n";

    std::string wait;
    std::getline( std::cin, wait );

    } // function main


    Comment

    • BobR

      #3
      Re: error trying to toupper string


      Jim Langston wrote in message ...
      >
      >This compiles and runs for me with the expected output of
      >HELLO
      >
      >I'm using Micorost Visual C++ .net 2003
      >
      >#include <iostream>
      >#include <string>
      >
      >std::string toupper(std::st ring S){
      for (int i=0; i < S.length(); ++i){
      S[i]=toupper(S[i]);
      }
      return S;
      }
      >
      >int main(){
      std::string MyString("Hello ");
      std::cout << toupper( MyString ) << "\n";
      std::string wait;
      std::getline( std::cin, wait );
      >} // function main
      Hi Jim,
      You should kick up the warning level a notch or two:

      //for( int i=0; i < S.length(); ++i){
      // [Warning] comparison between signed and unsigned integer expressions
      // ....or is type 'int' unsigned on your machine? <G>

      for( size_t i(0); i < S.size(); ++i){

      // or 'std::size_t'. 'std::string::s ize_type' is worth a typedef (and I hate
      typedef's!!<G>)

      Otherwise, works fine on MinGW(GCC3.3.1) .

      Now, put 'toupper' (as is) in a class, and see if it still works as
      advertised (call from inside the class).

      class ToUpTest{ public:
      void Print(std::stri ng str, std::ostream &sos){
      sos<<toupper(st r)<<std::endl;
      return;
      } // Print(string,os tream&)
      std::string toupper( std::string S ){
      for( size_t i(0); i < S.size(); ++i ){
      // S[ i ] = toupper( S[ i ] );
      // In member function
      //`std::string ToUpTest::toupp er( [snip] )':
      // error: invalid conversion from `char' to `const char*'
      // error: initializing argument 1 of [snip]
      // error: cannot convert `std::string' to `char' in assignment

      S[ i ] = std::toupper( S[ i ] ); // even '::toupper()'
      }
      return S;
      } // toupper(string)
      };

      { // main() or function
      std::string MyString("Hello ");
      // std::cout << toupper( MyString ) << "\n";

      ToUpTest TuT;
      TuT.Print( MyString, std::cout );
      }

      See the OPs problem now?
      --
      Bob R
      POVrookie


      Comment

      Working...