Thread: Question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    13

    Question

    I have a program and one of the things i need to do is search through the list of movies and find all the ones that have a certain word in it. Any ideas on how i could do this? Thank you.

  2. #2

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by bumfluff
    that's a c library. don't use it.

    OP, when you say "list of movies", what do you mean? a file with a bunch of names? a directory of mpegs?

    post more info on the problem and tell us what you've tried so far.

    BTW "Question" is just about the worst thread title ever. if you learn how to ask smart questions you're more likely to get an answer.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    13
    The list of movies is in a .txt file. For example :

    Meet Joe Black
    Meet the Parents
    Someting about Mary
    Gone in 60 seconds
    Father of the Bride

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    fstream in(" \\insertfile name here.
    string s;
    while(getline(in,s,'\n')){
    if(s.find("seconds")!=string::npos)
    cout<<s;
    }
    Code:
    
    
    but remember one thing Joe is not the same as joe.
    Last edited by qqqqxxxx; 03-24-2006 at 01:14 AM.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by ChaosEngine
    BTW "Question" is just about the worst thread title ever.
    Second to "Urgent Question Please Help", eh?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by qqqqxxxx
    fstream in(" \\insertfile name here.
    string s;
    while(getline(in,s,'\n')){
    if(s.find("seconds")!=string::npos)
    cout<<s;
    }
    Code:
    
    
    but remember one thing Joe is not the same as joe.
    Please give up at life now.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, here's one way you can do it... note that it's inefficient:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    std::string capitalize(std::string&in);
    
    int main()
    {
    	std::string word;
    	std::string search;
    	int line=1;
    	char ch;
    
    	struct link;
    	struct link
    	{
    		int data;
    		link*next;
    	} *curr,*next,*head=new link;
    
    	curr=head;
    	curr->data=-1;
    	curr->next=0;
    	
    	std::fstream file("test.dat",std::ios::in);
    	
    	std::cout<<"Enter your seach word: ";
    	getline(std::cin,search,'\n');
    
    	while(file>>word)
    	{
    		file.get(ch);
    		if(capitalize(search)==capitalize(word))
    		{
    			curr->data=line;
    			next=new link;
    			curr->next=next;
    			curr=next;
    			curr->data=-1;
    			curr->next=0;
    		}
    		line=(ch=='\n'?line+1:line);
    	}
    
    	curr=head;
    	std::cout<<"Matches were found on line(s): ";
    
    	while(curr->next)
    	{
    		std::cout<<curr->data<<", ";
    		curr=curr->next;
    	}
    
    	std::cout<<std::endl;	
    	return 0;
    }
    
    std::string capitalize(std::string&in)
    {
    	for(unsigned int i=0;i<in.length();i++)
    	{
    		in.at(i)=toupper(in.at(i));
    	}
    	return in;
    }
    and don't even try to take this as your own work... you would most certainly fail the assignment if you handed this in

    you'd best ignore the curr/head/data/link stuff and figure out your own way to record the results - I just put that in there to ensure you didn't get copyable code... sure, it works, but it's only half a program... (don't take that wrong, the searching algo is complete, it's the linked list that's not finished.)

    searching algorithms can get fairly complex, as touched upon in this wiki article, but on small amounts of data like this, you probably aren't looking for a very good algorithm.
    Last edited by major_small; 03-24-2006 at 10:01 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM