error trying to toupper string

Discussion in 'C++' started by sandy@murdocks.on.ca, Nov 29, 2006.

  1. Guest

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

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

    return S;
    }

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

    `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
    _CharT*, const _Alloc&) [with _CharT = char, _Traits =
    std::char_traits<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.
     
    , Nov 29, 2006
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    >I am trying to upper case a string, so I have this method:
    >
    > string FileSystem::toupper(string S)
    > {
    > for (int i=0; i<S.length(); ++i)
    > {
    > S=toupper(S);
    > }
    >
    > return S;
    > }
    >
    > I get the errors:
    > invalid conversion from `char' to `const char*'
    >
    > `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
    > _CharT*, const _Alloc&) [with _CharT = char, _Traits =
    > std::char_traits<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::string S)
    {
    for (int i=0; i < S.length(); ++i)
    {
    S=toupper(S);
    }

    return S;
    }

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

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

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

    } // function main
     
    Jim Langston, Nov 29, 2006
    #2
    1. Advertising

  3. BobR Guest

    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::string S){
    > for (int i=0; i < S.length(); ++i){
    > S=toupper(S);
    > }
    > 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::size_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::string str, std::eek:stream &sos){
    sos<<toupper(str)<<std::endl;
    return;
    } // Print(string,ostream&)
    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::toupper( [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
     
    BobR, Nov 29, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri

    String.ToUpper

    Kerri, Oct 26, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,069
    Chris Botha
    Oct 27, 2003
  2. ben

    toupper wont work

    ben, Feb 21, 2004, in forum: C++
    Replies:
    5
    Views:
    3,147
    Dietmar Kuehl
    Feb 25, 2004
  3. Replies:
    4
    Views:
    514
  4. gaga
    Replies:
    16
    Views:
    658
    Daniel Kay
    Sep 23, 2007
  5. David RF

    toupper UTF8 string

    David RF, Sep 24, 2009, in forum: C Programming
    Replies:
    9
    Views:
    1,988
    Keith Thompson
    Sep 27, 2009
Loading...

Share This Page