|
|
#1 (permalink) | |
|
Expert Zuner
|
ok, this is programing help, but afaik we don't have a programing section
I am stuck on a particular function of a program I am working on. "void searchRecords() This void function should prompt the user for search text, then open C:\PhoneBook.txt for input, read all records in the file, and display all lines that contain the search text. Display a count of the records that matched the search text. Display a different message if zero, one, or more matches are found." an example line in phonebook.txt would be allen 407566432 any ideas? I know I need to use string.find function to do it, just not sure how.
__________________
Quote:
![]() ![]() |
|
|
|
| Advertisement | [Remove Advertisement] | ||
|
|
|
|
#2 (permalink) |
|
Resident Huge Arab
Retired Staff
Zune Priest Join Date: May 2008
Location: Invicta, Planet Bob
Posts: 6,800
Reputation: 1142
|
You can always stuff it here: http://www.zuneboards.com/forums/dev...scussions-172/
![]() Gimme a sec here, I think I have something like it somewhere....
__________________
![]() |
|
|
|
|
#3 (permalink) | |
|
Expert Zuner
|
does this look right?
void searchrecords() { ifstream indata; string rec, line1; int match; cout << "Please enter the item to search for: "; cin >> rec; indata.open("phonebook.txt"); while (!indata.eof()) { getline(indata, line1); if (line1.find(rec) != string ::npos) { match++; cout << line1 << endl; } } if(match == 0) cout << "0 matches found." << endl; if(match == 1) cout << "1 match found." << endl; if(match >= 1) cout << match << " matches found." << endl; indata.close("phonebook.txt"); }
__________________
Quote:
![]() Last edited by ChurchedAtheist; 12-02-2008 at 10:59 AM.. ![]() |
|
|
|
![]() |
| Thread Tools | |
|
|
| |