944,092 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4689
  • C++ RSS
Mar 29th, 2005
0

need to find occurances in a string

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
mb1
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mb1 is offline Offline
7 posts
since Mar 2005
Mar 29th, 2005
0

Re: need to find occurances in a string

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. string::size_type index = s.find ( "test" );
  2.  
  3. if ( index != string::npos )
  4. cout<< s.substr ( index ) <<endl;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 29th, 2005
0

Re: need to find occurances in a string

So my getline() has already returned to string and what I need is to code for the occurances of a word in the string?
mb1
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mb1 is offline Offline
7 posts
since Mar 2005

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: Arrow to select item
Next Thread in C++ Forum Timeline: Exercise using: unsigned int datecode(int year, int month, int day);





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


Follow us on Twitter


© 2011 DaniWeb® LLC