| | |
need to find occurances in a string
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 7
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
/* 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
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:
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)
int index = s.find ( "test" ); if ( index != string::npos ) cout<< s.substr ( index ) <<endl;
C++ Syntax (Toggle Plain Text)
string::size_type index = s.find ( "test" ); if ( index != string::npos ) cout<< s.substr ( index ) <<endl;
New members chased away this month: 5
![]() |
Similar Threads
- how to find length of a string (C++)
- vector<string> - way to find longest string? (C++)
- using stringstream for tokenizing a string (C++)
- Program with many errors (C++)
- String operatios (C++)
- how can i find out some char from string? (C++)
Other Threads in the C++ Forum
- Previous Thread: while(true) loop + getline() = infinite loop??
- Next Thread: Exercise using: unsigned int datecode(int year, int month, int day);
Views: 3726 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






