help please!

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

    #1

    help please!

    why does this not compile?

    std::string test;
    std::string tester="integer is a number";
    int strlen = tester.length() ;
    for(int i = 0; i<strlen-1;i++)
    {
    test[i]=tester[i];
    if(test=="integ er")
    {cout<<"integer found!";}
    }

    I am trying to pull extract individual words from a sentence.

  • John Harrison

    #2
    Re: help please!

    idikoko wrote:[color=blue]
    > why does this not compile?
    >
    > std::string test;
    > std::string tester="integer is a number";
    > int strlen = tester.length() ;
    > for(int i = 0; i<strlen-1;i++)
    > {
    > test[i]=tester[i];
    > if(test=="integ er")
    > {cout<<"integer found!";}
    > }
    >
    > I am trying to pull extract individual words from a sentence.
    >[/color]

    I can't see any reason that would not compile. If you are getting
    compilation errors then you should quote the error messages you get and
    the lines they apply to.

    However I can see why the code would not work. This line

    test[i]=tester[i];

    is wrong because test is a string with zero length, so test[i] is trying
    to access a character in the string that doesn't exist. Instead you want

    test.push_back( tester[i]);

    push_back adds a character to the end of a string.

    John

    Comment

    • meagar

      #3
      Re: help please!

      For future questions, it would help to include the error message your
      compiler is giving you.

      At a glance, I'd guess you might be redefining 'strlen', which is
      already a standard function for finding the length of c-style strings.

      Also, while it won't cause a compiler error, the line "test[i] =
      tester[i]" is a serious logic error. The [] operator only provides
      unchecked access and doesn't itself grow the string to provide storage
      for random assignment. You might try test += test[i], which will grow
      the test string dynamically by one character each time through the loop.

      Comment

      • John Harrison

        #4
        Re: help please!

        meagar wrote:[color=blue]
        > For future questions, it would help to include the error message your
        > compiler is giving you.
        >
        > At a glance, I'd guess you might be redefining 'strlen', which is
        > already a standard function for finding the length of c-style strings.
        >[/color]

        That won't cause a compile error. The local name will just hide the
        global one.

        john

        Comment

        • idikoko

          #5
          Re: help please!

          I got this new error when I used the push_back function.

          'std::basic_str ing<_Elem,_Trai ts,_Ax>::push_b ack' : cannot convert
          parameter 1 from 'std::string' to 'char'
          with
          [
          _Elem=char,
          _Traits=std::ch ar_traits<char> ,
          _Ax=std::alloca tor<char>
          ]

          Comment

          • Ian

            #6
            Re: help please!

            idikoko wrote:[color=blue]
            > I got this new error when I used the push_back function.
            >
            > 'std::basic_str ing<_Elem,_Trai ts,_Ax>::push_b ack' : cannot convert
            > parameter 1 from 'std::string' to 'char'
            > with
            > [
            > _Elem=char,
            > _Traits=std::ch ar_traits<char> ,
            > _Ax=std::alloca tor<char>
            > ]
            >[/color]
            Quote your code, did you type it correctly? Looks like you may have
            left off the [i]

            Ian

            Comment

            • idikoko

              #7
              Re: help please!

              yeah thanks it worked thanks

              Comment

              • Old Wolf

                #8
                Re: help please!

                John Harrison wrote:[color=blue]
                > meagar wrote:[color=green]
                >> For future questions, it would help to include the error message your
                >> compiler is giving you.
                >>
                >> At a glance, I'd guess you might be redefining 'strlen', which is
                >> already a standard function for finding the length of c-style strings.[/color]
                >
                > That won't cause a compile error. The local name will just hide the
                > global one.[/color]

                I think the names of C standard library functions are all reserved
                identifiers at any scope. This is in case the standard library
                functions are implemented with macros, eg:

                #define strlen(s) ( find_eos(s) - (s) )

                I can't find exact wording to support this, but N869 7.1.4#1 says:
                "Any function declared in a header may be additionally
                implemented as a function-like macro defined in the header"

                Comment

                Working...