need to find occurances in a string

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 7
Reputation: mb1 is an unknown quantity at this point 
Solved Threads: 0
mb1 mb1 is offline Offline
Newbie Poster

need to find occurances in a string

 
0
  #1
Mar 29th, 2005
  1. /* Write a program that requests a file description (word)
  2. and a string from the user then states the number of occurances
  3. of the string in the file. (We will search for the word 'the'.)
  4. If the file does not exist, the program should state this
  5. to the user.*/
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <conio.h>
  10. #include <string>
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15. //Step01: Declare Memory Locations
  16. ifstream InFile;
  17. ofstream OutFile;
  18. string MyWord; //Location of text to be searched for
  19. //Step02: Initialize Memory Locations
  20. InFile.open("C:/Temp/TestData1.txt");/*Location of file
  21. to be read*/
  22. OutFile.open("C:/Temp/Testdata2");
  23. int Index = 0; //Counter for occurances
  24. //Step03: Do the Work
  25. cout << "Please enter text: ";
  26. cin >> MyWord;
  27.  
  28. if (!InFile.is_open())
  29. {
  30. cerr << "File does not exist" << endl;
  31. getch();
  32. exit(0);
  33. }
  34. getline(cin, MyWord);
  35. {
  36. string word = MyWord.find("the");
  37. Index++;
  38. }
  39. //Step04: Do the Work
  40. cout << endl;
  41. getch();
  42. return 0;
  43. }

This line, string word = MyWord.find("the"); generates this error:

error C2440: 'initializing' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>::size_type' to 'std::basic_string<_Elem,_Traits,_Ax>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
and
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

Any help? Please point out any other errors, please

Thanks,

MB1
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: need to find occurances in a string

 
0
  #2
Mar 29th, 2005
find returns an index, not a string. For example, if the string is "atest" and you search for "test", find would return 1. Then you could do this:
  1. int index = s.find ( "test" );
  2.  
  3. if ( index != string::npos )
  4. cout<< s.substr ( index ) <<endl;
And "test" would be printed. Note that while I used int for the index, the proper type is string::size_type:
  1. string::size_type index = s.find ( "test" );
  2.  
  3. if ( index != string::npos )
  4. cout<< s.substr ( index ) <<endl;
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 7
Reputation: mb1 is an unknown quantity at this point 
Solved Threads: 0
mb1 mb1 is offline Offline
Newbie Poster

Re: need to find occurances in a string

 
0
  #3
Mar 29th, 2005
So my getline() has already returned to string and what I need is to code for the occurances of a word in the string?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3726 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC