943,935 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5205
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 11th, 2008
0

Re: c++ find word from text file

thank you
Reputation Points: 5
Solved Threads: 0
Newbie Poster
ojung is offline Offline
10 posts
since Nov 2008
Dec 11th, 2008
0

Re: c++ find word from text file

Quote originally posted by ddanbe ...
C++ has a string class which contains a compare method. You certainly have some documentation where you can get info about those. If you can find it you can even do better.
I can understand that is is all new to you, and that it is difficult to grasp it all, but why use char array when there is a string class? Why do you refuse to read or look up documentation?
You do #include <string> so why don't you use it?
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Dec 11th, 2008
0

Re: c++ find word from text file

i doesn't return to loop ,, how can i do??
Reputation Points: 5
Solved Threads: 0
Newbie Poster
ojung is offline Offline
10 posts
since Nov 2008
Dec 11th, 2008
-1

Re: c++ find word from text file

i got a result:::

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
const int i=20;
char word[i];
int Counter;
cout<<"Enter word to search \n";
cin.get(word,i);


char fileName[80];
string buffer; // for user input
cout << "Enter file name: ";
cin >> fileName;


ifstream fin(fileName);
if(!fin.is_open())
{
cout << "Unable to open file";
system("pause");
exit(1);
}

Counter=0;
while(fin!='\0')
{
getline(fin,buffer);

size_t a = buffer.find(word,0);
if (a != string::npos)
{
cout<<buffer<<endl;
Counter++;
size_t b = a + 1;
a = buffer.find (word, b);
}
}
if(Counter>1)

cout << "Total "<<Counter<<" tLines\n";
else
cout << "Total "<<Counter<<" Line\n";
system("pause");
return 0;
}
____________________________________________

So,how can i link this program to dos??
Reputation Points: 5
Solved Threads: 0
Newbie Poster
ojung is offline Offline
10 posts
since Nov 2008
Dec 12th, 2008
0

Re: c++ find word from text file

Click to Expand / Collapse  Quote originally posted by ojung ...
cout<<"Enter word to search \n";
cin>>word[i];
that should be
C++ Syntax (Toggle Plain Text)
  1. cin >> word;
, because word[i] is a char, and word is a char array
Quote ...
while(dict.getline(text,300))

{
if(word[j]!=text[j])
{
Likewise this should be
C++ Syntax (Toggle Plain Text)
  1. ...
  2. if(string(text).find(word) != string::npos){
  3. ...
, otherwise you are comparing the first character only, and not the whole word
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Mar 14th, 2010
-1
Re: c++ find word from text file
you cant enter the char array as
C++ Syntax (Toggle Plain Text)
  1. "cout<<"Enter word to search \n";
  2. cin>>word[i];"

You should get the charter array char by char so Use
C++ Syntax (Toggle Plain Text)
  1. for(i=0;i<30;i++)
  2. cin>>word[i];

Here you must enter 30 chars exactly.
Last edited by Nick Evan; Mar 14th, 2010 at 9:15 am. Reason: Added code-tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onksssss is offline Offline
1 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: finding first word having a particular char 'w' in a sentence
Next Thread in C++ Forum Timeline: C++ class problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC