Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Inputing variable into string.find

Inputing variable into string.find

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasehelptutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gregarion
    wrote on last edited by
    #1

    Hey guys, could i check with something What i would like to do is to be able to input a number and using that number as the index , it will start finding where a certain line is (in this case yht) from there. for example

    int main ()
    {
    string s("abcdefghijleeeeeeeyhtpr") ;

    string::size_type loc2 = s.find( "yht" , 0 ) ;

    cout << loc2 << endl ;

    the input will be 18 , as it starts counting from the start of the string. What i would like to know is, am i able to set a variable in the find syntax? instead of putting 0 , can i declare a variable and then can the sting.find() read it? So , if i were to input in 3 , how can i declare this and then put it into the find statement? Hope for some help. thanks

    CPalliniC G D 3 Replies Last reply
    0
    • G gregarion

      Hey guys, could i check with something What i would like to do is to be able to input a number and using that number as the index , it will start finding where a certain line is (in this case yht) from there. for example

      int main ()
      {
      string s("abcdefghijleeeeeeeyhtpr") ;

      string::size_type loc2 = s.find( "yht" , 0 ) ;

      cout << loc2 << endl ;

      the input will be 18 , as it starts counting from the start of the string. What i would like to know is, am i able to set a variable in the find syntax? instead of putting 0 , can i declare a variable and then can the sting.find() read it? So , if i were to input in 3 , how can i declare this and then put it into the find statement? Hope for some help. thanks

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      I I'm missing something or you are asking for (I cannot believe it...)

      string::size_type s = 3;
      s.find( "yht" , s ):

      ? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • G gregarion

        Hey guys, could i check with something What i would like to do is to be able to input a number and using that number as the index , it will start finding where a certain line is (in this case yht) from there. for example

        int main ()
        {
        string s("abcdefghijleeeeeeeyhtpr") ;

        string::size_type loc2 = s.find( "yht" , 0 ) ;

        cout << loc2 << endl ;

        the input will be 18 , as it starts counting from the start of the string. What i would like to know is, am i able to set a variable in the find syntax? instead of putting 0 , can i declare a variable and then can the sting.find() read it? So , if i were to input in 3 , how can i declare this and then put it into the find statement? Hope for some help. thanks

        G Offline
        G Offline
        gregarion
        wrote on last edited by
        #3

        Sorry, what i meant is this

        int main ()
        {
        string s("abcdefghijleeeeeeeyhtpr") ;

        int now = 6 ;
        int nowe = 3 ;

        int result = nowe - now ;

        string::size_type f = result ;
        string::size_type loc2 = s.find( "yht" , f ) ;

        cout << loc2 << endl ;
        cout << f << endl ;

        when i tried running this , i got the result displaying (4294967295 ). why is this? are we not allowed to use the variable (result) and put it to size_type f = result? is it not the same as size_type f = 3?

        CPalliniC D 2 Replies Last reply
        0
        • G gregarion

          Hey guys, could i check with something What i would like to do is to be able to input a number and using that number as the index , it will start finding where a certain line is (in this case yht) from there. for example

          int main ()
          {
          string s("abcdefghijleeeeeeeyhtpr") ;

          string::size_type loc2 = s.find( "yht" , 0 ) ;

          cout << loc2 << endl ;

          the input will be 18 , as it starts counting from the start of the string. What i would like to know is, am i able to set a variable in the find syntax? instead of putting 0 , can i declare a variable and then can the sting.find() read it? So , if i were to input in 3 , how can i declare this and then put it into the find statement? Hope for some help. thanks

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          gregarion wrote:

          What i would like to do is to be able to input a number and using that number as the index

          What about:

          int index;
          cin >> index;
          string::size_type loc2 = s.find("yht", index);

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          G 1 Reply Last reply
          0
          • G gregarion

            Sorry, what i meant is this

            int main ()
            {
            string s("abcdefghijleeeeeeeyhtpr") ;

            int now = 6 ;
            int nowe = 3 ;

            int result = nowe - now ;

            string::size_type f = result ;
            string::size_type loc2 = s.find( "yht" , f ) ;

            cout << loc2 << endl ;
            cout << f << endl ;

            when i tried running this , i got the result displaying (4294967295 ). why is this? are we not allowed to use the variable (result) and put it to size_type f = result? is it not the same as size_type f = 3?

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            well, nowe-now is -3, then cast it to unsigned... :rolleyes:

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • G gregarion

              Sorry, what i meant is this

              int main ()
              {
              string s("abcdefghijleeeeeeeyhtpr") ;

              int now = 6 ;
              int nowe = 3 ;

              int result = nowe - now ;

              string::size_type f = result ;
              string::size_type loc2 = s.find( "yht" , f ) ;

              cout << loc2 << endl ;
              cout << f << endl ;

              when i tried running this , i got the result displaying (4294967295 ). why is this? are we not allowed to use the variable (result) and put it to size_type f = result? is it not the same as size_type f = 3?

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              gregarion wrote:

              when i tried running this , i got the result displaying (4294967295 ). why is this?

              Perhaps because f (and result) is -3.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              1 Reply Last reply
              0
              • D David Crow

                gregarion wrote:

                What i would like to do is to be able to input a number and using that number as the index

                What about:

                int index;
                cin >> index;
                string::size_type loc2 = s.find("yht", index);

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                G Offline
                G Offline
                gregarion
                wrote on last edited by
                #7

                Thanks guys, i got it. the issue was i thought that the output will start counting from the index i told it to. it only searches starting on the index, but output will be based from counting from the start of the file. Thanks!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups