Thread: searching help? (find function)

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    searching help? (find function)

    sorry if this has been asked before, but i am confused about a code which is posted on the tutorial page:
    Code:
        string input;
        int i = 0;
        int cat_appearances = 0;
    
        getline(cin, input, '\n');
    
        for(i = input.find("cat", 0); i != string::npos; i = input.find("cat", i))
        {
            cat_appearances++;
            i++;  // Move past the last discovered instance to avoid finding same
                  // string
        }
        cout<<cat_appearances;
    why do i have to use loops to use the find function? i tried using if and it still works; it even gives the same output! here's the code which i modified from the tutorial using if:

    Code:
    #include<iostream>
    #include<Windows.h>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        string input;
        int i = 0;
        int cat_appearances = 0;
    
        if(input.find("cat", 0) != string::npos) cat_appearances++;
    
        cin.get();
        return 0;
    }
    can someone explain it to me? thanks in advance though ! ^^

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Use the tutorial code as it is, type in 'catacat' and observe the output
    Now add a similar getline(cin, input, '\n'); just before your if (so you can type), and a cout << cat_appearances; just below (so you can see the count). Compile and run it and type in the same 'catacat'

    You should be able to hazard a guess at what the tutorial code is doing that your if is not.
    Remember 'if' does at most one sequence of actions, for loops can do as many sequences as needed until the condition in the middle (the 'i != string::npos' bit in the tutorial) is false.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by aquatorrent View Post
    i tried using if and it still works; it even gives the same output!
    Sure, if the word contains zero or one "cat". If it contains more than one, the output will not be the same, and please do not bother trying to claim otherwise because no one will believe that (with regard specifically to those two examples, it is just not possible).

    Of course, the second example is probably not what you really used, since "string input" remains uninitialized throughout. Always try and post your actual code, not some approximation of it.
    Last edited by MK27; 03-26-2012 at 07:59 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by MK27 View Post
    Sure, if the word contains zero or one "cat". If it contains more than one, the output will not be the same, and please do not bother trying to claim otherwise because no one will believe that (with regard specifically to those two examples, it is just not possible).

    Of course, the second example is probably not what you really used, since "string input" remains uninitialized throughout. Always try and post your actual code, not some approximation of it.
    yep, i know. and that's why i'm asking this. i must have done something wrong and thanks to you guys, i know what's wrong. i tested only with one 'cat' word so that's why i confused with this

    (and i just messed up with the code right before i posted this. that's why there's no getline and cout function(because i forgot to put it too). sorry :P)

    anyway, since the problem is solved, can someone tell me how to lock this thread? thanks ^^

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We don't lock threads here. If your question is answered, there is no need for further action on your part. You can just the thread be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you find header of a function?
    By yougene in forum C Programming
    Replies: 4
    Last Post: 06-02-2009, 04:20 PM
  2. find string function
    By barneygumble742 in forum C++ Programming
    Replies: 1
    Last Post: 07-18-2005, 04:18 PM
  3. Function To Find Factorials!!! Please Help
    By adzza69 in forum C++ Programming
    Replies: 22
    Last Post: 08-20-2004, 06:39 PM
  4. the find member function
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2003, 04:03 PM
  5. File searching function
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-04-2001, 05:24 PM