Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,567 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,652 people online right now. Registration is fast and FREE... Join Now!




find and locate specific word in a text after you read it into the pro

 
Closed TopicStart new topic

find and locate specific word in a text after you read it into the pro, read content

Inarius
30 Oct, 2007 - 04:23 PM
Post #1

New D.I.C Head
*

Joined: 30 Oct, 2007
Posts: 18


My Contributions
removed

This post has been edited by Inarius: 31 Oct, 2007 - 11:08 AM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Find And Locate Specific Word In A Text After You Read It Into The Pro
30 Oct, 2007 - 04:35 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
The method find() returns an index within a string where the "searched-for" substring was found. In this case, you are outputting the index where the word "explosive" was found - and judging from the output the program gave you, you have the word explosive as the first word in the 6th line in the file. The '-1' value you're seeing is a the value given by string::npos - which simply means "no position," i.e. that the string wasn't found on that line. Remember, you're searching through the file one line at a time, and there's nothing in the current program to keep track of which line you're currently on.

If you're trying to figure out which line number the string occurs on, you'll need to add something else to this. You could use a counter variable that you initialize before the input loop, and then only output the counter's variable when the value returned from the find() method is not equal to string::npos:
CODE

    int counter=0;
    while (getline(in_stream, line)) {
        counter++;
        int x = line.find("explosive");
        if (x!=string::npos) {
            cout << counter << endl;
        }
    }

Hope that helps,

-jjh

This post has been edited by jjhaag: 30 Oct, 2007 - 04:36 PM
User is offlineProfile CardPM
+Quote Post

Inarius
RE: Find And Locate Specific Word In A Text After You Read It Into The Pro
30 Oct, 2007 - 04:42 PM
Post #3

New D.I.C Head
*

Joined: 30 Oct, 2007
Posts: 18


My Contributions
thank you... that worked out well wink2.gif i shall forever remember this counter code from this day forth without fail! oh can you also explain to me one more thing? what does this line do?
CODE
  if (x!=string::npos)

its saying if x is not at string no position????? thanks

This post has been edited by Inarius: 30 Oct, 2007 - 04:50 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Find And Locate Specific Word In A Text After You Read It Into The Pro
30 Oct, 2007 - 05:08 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
No problem smile.gif

And as for the line if (x!=string::npos)...

string:: just says that you're using the string namespace - which you need to do because the value npos is defined in that namespace.

The find() function returns an index of where the substring was found within your string, and it is this returned index value that gets assigned to x. When the find() method doesn't find the substring, it needs some way to tell you this - so it returns string::npos, which is a negative value and wouldn't otherwise make any sense as an index into a string. It's basically just an arbitrary value that indicates the case when the string isn't found.

If you find the namespace thing confusing, just remember that you are already using the std namespace in your program, which is where objects like cin, cout, and endl are defined. If you didn't have the line using namespace std; before your main routine, you would have to make your output calls like this:
CODE

std::cout << x << std::endl;

where you explicitly state which namespace you are talking about when you refer to cout and endl.

Oh, and one final point - you should define an int return type for main, not void, and you would then need to have it return an int at the completion of the program:
CODE

int main() {
    //...
    //...
    return 0;  //last line in main()
}


Hope that helps smile.gif

-jjh

This post has been edited by jjhaag: 30 Oct, 2007 - 05:08 PM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Find And Locate Specific Word In A Text After You Read It Into The Pro
30 Oct, 2007 - 05:11 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Among a few more obscure uses, npos is the value returned if the searched for substring is not found in the main string. The statement effectively says
QUOTE

if the value of x does not equal a failed find operation


User is offlineProfile CardPM
+Quote Post

Inarius
RE: Find And Locate Specific Word In A Text After You Read It Into The Pro
30 Oct, 2007 - 05:22 PM
Post #6

New D.I.C Head
*

Joined: 30 Oct, 2007
Posts: 18


My Contributions
o. i see, thanks all wink2.gif

This post has been edited by Inarius: 30 Oct, 2007 - 05:22 PM
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/7/09 10:17PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month