string lengths as integers.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Schumacher

    string lengths as integers.

    I've been trying to use the following line of code to take a character
    from the end of the string:

    char b = pStr.substr(pSt r.length, 1);

    However the compiler (the Visual C++ one) returns the following error:
    'substr' : cannot convert parameter 1 from 'unsigned int
    (void) const' to 'unsigned int' Conversion is a valid
    standard conversion, which can be performed implicitly or by
    use of static_cast, C-style cast or function-style cast

    I tried static_casting, C-Style and function-style to assign an int
    variable the value of the string length, but I got the exact same
    error every time.
    I also tried use strlen(pStr) and got a similar error (only the data
    type of the original was different).

    Is there any way to make this work?
    Any help is greatly appreciated!

    -==Kensu==-
  • Ivan Vecerina

    #2
    Re: string lengths as integers.

    "Chris Schumacher" <kensu__@hotmai l.com> wrote in message
    news:kno8rvknau l8ml5tldv45hjgn 61f245tu5@4ax.c om...[color=blue]
    > I've been trying to use the following line of code to take a character
    > from the end of the string:
    >
    > char b = pStr.substr(pSt r.length, 1);[/color]
    1) Did you mean:
    pStr.length()
    ?
    std::string::le ngth is a member function, not a data member.

    2) in C++, char is not equivalent to a 1-character string.
    To get the n-th character of a string, you need to
    use the [] operator instead of substr.
    And character indexes are 0-based, so the last character
    is at position length()-1 :
    char b = pStr[ pStr.length()-1 ];


    hth
    --
    Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets



    Comment

    • Andy Kashyap

      #3
      Re: string lengths as integers.


      Length is a function, not a scalar.
      Try pStr.substr(pSt r.length(), 1)

      --
      Andy Kashyap
      to reply, sed -e 's/nospam//g' emailAddress



      "Chris Schumacher" <kensu__@hotmai l.com> wrote in message
      news:kno8rvknau l8ml5tldv45hjgn 61f245tu5@4ax.c om...[color=blue]
      > I've been trying to use the following line of code to take a character
      > from the end of the string:
      >
      > char b = pStr.substr(pSt r.length, 1);
      >
      > However the compiler (the Visual C++ one) returns the following error:
      > 'substr' : cannot convert parameter 1 from 'unsigned int
      > (void) const' to 'unsigned int' Conversion is a valid
      > standard conversion, which can be performed implicitly or by
      > use of static_cast, C-style cast or function-style cast
      >
      > I tried static_casting, C-Style and function-style to assign an int
      > variable the value of the string length, but I got the exact same
      > error every time.
      > I also tried use strlen(pStr) and got a similar error (only the data
      > type of the original was different).
      >
      > Is there any way to make this work?
      > Any help is greatly appreciated!
      >
      > -==Kensu==-[/color]


      Comment

      • Catalin Pitis

        #4
        Re: string lengths as integers.

        Try
        char b = *pstr.rbegin();


        Catalin

        "Chris Schumacher" <kensu__@hotmai l.com> wrote in message
        news:kno8rvknau l8ml5tldv45hjgn 61f245tu5@4ax.c om...[color=blue]
        > I've been trying to use the following line of code to take a character
        > from the end of the string:
        >
        > char b = pStr.substr(pSt r.length, 1);
        >
        > However the compiler (the Visual C++ one) returns the following error:
        > 'substr' : cannot convert parameter 1 from 'unsigned int
        > (void) const' to 'unsigned int' Conversion is a valid
        > standard conversion, which can be performed implicitly or by
        > use of static_cast, C-style cast or function-style cast
        >
        > I tried static_casting, C-Style and function-style to assign an int
        > variable the value of the string length, but I got the exact same
        > error every time.
        > I also tried use strlen(pStr) and got a similar error (only the data
        > type of the original was different).
        >
        > Is there any way to make this work?
        > Any help is greatly appreciated!
        >
        > -==Kensu==-[/color]


        Comment

        Working...