Thread: file word searcher

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    file word searcher

    Hi, im new here. I cant seem to get this code to work, the goal is to look into a file a search for multiple words. Im a newbie and this is my first c++ course

    [tag]
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    int main ()
    {
    string file;
    string word[1000];
    int count=0;
    string text = "";
    int alone = 0;
    int times = 0;
    int inward = 0;
    int j = 0;
    int q = 0;
    string stuff;
    
    cout << "enter file name.\n";
    cin >> file;
    while(stuff!="done")
    {
    cout << "enter search word. Type done when finished\n";
    cin >> stuff;
    if(stuff!= "done")
    {
    word[count] = stuff;
    count++;
    }
    }
    char fileName[file.length() + 1];
    file.copy(fileName, file.length());
    ifstream in(fileName);
    char c;
    while(in.get(c))
    {
     text += c;               
    }
    for(int i=0;i < count;i++)
    {        
    times = 0;
    alone = 0;
    int index = 0;
    int length = word[i].length();
    string text2 = text;
    
    while(text2.find(word[i])<-1)
    {
    if(text2.find(word[i]+" ")< 0 && q == 2)
    {
    q = 2;
    alone++;
    times++;
    text2.substr(length);
    }
    
     if(text2.find(word[i])<-1)
    {                    
     times++;
     string hi =" " + word[i] + " ";
     if(text2.find(hi) <-1)
    {
    
     alone++;
    }
    
    }
    
    
    index = text2.find(word[i]);
    text2=text2.substr(index+length); 
     
    } 
    cout<<text2<<"\n";
    
    cout<< word[i]<<" was found alone " <<alone<< " time(s).\n";
    cout << "";
    cout<< word[i]<<" was found in another word " <<times - alone <<" time(s). \n";
    cout <<"";
    cout << "Together it was found "<< times<< " times.\n";
    cout << "\n";
    }
    
    
    
    system("pause");
    return 0;
    }
    [/tag]
    this code

    Code:
    [tag]
     if(text2.find(word[i])<-1)
    {                    
     times++;
     string hi =" " + word[i] + " ";
     if(text2.find(hi) <-1)
    {
    
     alone++;
    }
    
    }
    [/tag]]
    is where im having trouble
    The file says, the rain in spain falls mainly in the plains.
    the search word i use is "in"
    It finds it alone 5 times and finds it in another word once
    It should find it alone twice and in another word 4 times.
    I think the problem is, it's always going into the alone++ code, iv'e been working on this code for a long while and have been to many friends for help. Ths forum is my last hope.
    im sorry if this is hard to read im real new at posting code,hopefully the tags are all set and it's readable.
    Sorry for being a newb and thank you for reading.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Instead of reading in each character into a single string that holds the whole file, why not read in each word from the file separately and store that into some data structure. A map or vector (to be sorted later) would be ideal, but it depends on what you've learned already.

    BTW,
    Code:
    char fileName[file.length() + 1];
    file.copy(fileName, file.length());
    ifstream in(fileName);
    can and should be:
    Code:
    ifstream in(file.c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM