User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 425,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,632 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2358 | Replies: 2
Reply
Join Date: Mar 2005
Posts: 7
Reputation: mb1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mb1 mb1 is offline Offline
Newbie Poster

need to find occurances in a string

  #1  
Mar 29th, 2005
/* Write a program that requests a file description (word)
and a string from the user then states the number of occurances
of the string in the file. (We will search for the word 'the'.)
If the file does not exist, the program should state this 
to the user.*/
 
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
using namespace std;

int main()
{
	//Step01: Declare Memory Locations
	ifstream InFile;
	ofstream OutFile;
	string MyWord; //Location of text to be searched for
	//Step02: Initialize Memory Locations
	InFile.open("C:/Temp/TestData1.txt");/*Location of file 
	to be read*/
	OutFile.open("C:/Temp/Testdata2");
	int Index = 0; //Counter for occurances
	//Step03: Do the Work
	cout << "Please enter text: ";
	cin >> MyWord;
	 
	if (!InFile.is_open())
	{
		cerr << "File does not exist" << endl;
		getch();
		exit(0);
	}
    getline(cin, MyWord);
	{
		string word = MyWord.find("the");
		Index++;
	}
	//Step04: Do the Work
	cout << endl;
	getch();
	return 0;
}

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,324
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: need to find occurances in a string

  #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:
int index = s.find ( "test" );

if ( index != string::npos )
  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:
string::size_type index = s.find ( "test" );

if ( index != string::npos )
  cout<< s.substr ( index ) <<endl;
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Mar 2005
Posts: 7
Reputation: mb1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mb1 mb1 is offline Offline
Newbie Poster

Re: need to find occurances in a string

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:43 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC