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.