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 456,446 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 2,659 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: 811 | Replies: 4
Reply
Join Date: Nov 2007
Posts: 3
Reputation: Dvb701 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Dvb701 Dvb701 is offline Offline
Newbie Poster

taking user input and searching a .txt file for matching records need a hint please

  #1  
Nov 20th, 2007
// David Barkman
// CSIS 123 - Murtha
// Program 13
//

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

void displayMenu();
void addRecord();
void searchRecords();

string userName;

int main()
{
// Prompt fior the user name
cout << "Please enter your name to begin: ";

// Input the user name
getline(cin,userName);

// Clear the screen
system("cls");

// Display the menu
displayMenu();

return 0;
} // end main()

void addRecord()
{
// Allocate memory storage
ofstream outFile;
string userString;

// Prompt the user for information
cout << "Please enter the information you wish to add to the file: " << endl;
cin.ignore(100 , '\n');
getline(cin, userString);

// Open the file
outFile.open("c:\\PhoneBook.txt", ios::app);

// Write to the file
outFile << userString << endl;

// Close the file
outFile.close();

// Clear the screen
system("cls");

// Give feedback to the user
cout << "New record has been added." << endl;

} // end addRecord()

void searchRecords()
{
// Allocate memory storage
string userChoice;
ifstream inFile;
string tempString;
int i = 0;


// Prompt the user for search text
cout << "Please enter the text you wish to search for: " << endl;
getline(cin, userChoice);

// Open the file
inFile.open("c:\\PhoneBook.txt");

// Read from the file
while (!inFile.eof())
{
getline(inFile, tempString);
(tried userChoice.find not sure what to do)
cout << tempString << endl;
i++;

} // End While




} // end searchRecord()
void displayMenu()
{
// Allocate memory storage
int userSelection;

while (userSelection != 3)
{
cout << "***************************" << endl;
cout << userName << "'s Phone Book:" << endl;
cout << "1. Add Record" << endl;
cout << "2. Search for Record" << endl;
cout << "3. Quit" << endl;
cout << "***************************" << endl;

// Prompt user for selection
cout << "Your Selection: ";
cin >> userSelection;

// Clear the screen
system("cls");

switch(userSelection)
{
case 1:
addRecord();
break;
case 2:
searchRecords();
break;
case 3:
// End the Program
break;
default:
cout << "Please select 1, 2, or 3" << endl;
} // end Switch
} // end while

} // end displayMenu()

i cant seem to figure out how to write the void function void (searchRecords() ) of Search for matching records tried numerous things, what i need it to do is just search for a record and display it to the user how many matches found, thanks in advance for any assistance rendered
Last edited by Dvb701 : Nov 20th, 2007 at 12:58 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: taking user input and searching a .txt file for matching records need a hint plea

  #2  
Nov 20th, 2007
Originally Posted by Dvb701 View Post
i cant seem to figure out how to write the void function void (searchRecords() ) of Search for matching records tried numerous things, what i need it to do is just search for a record and display it to the user how many matches found, thanks in advance for any assistance rendered

As a new member, maybe you can help us out. Is there something we can do when someone becomes a new member so that they will read the rules about posting as requested? What would have enticed you to read them?

Just looking for a little input... Thanks.

Since you read the record into tempString , then tempString.find would work better.

Also read this about while (!inFile.eof()) (feof() is identical to .eof())
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: Dvb701 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Dvb701 Dvb701 is offline Offline
Newbie Poster

Re: taking user input and searching a .txt file for matching records need a hint please

  #3  
Nov 20th, 2007
sorry i wasnt aware i broke a rule i did read the "read this before posting" before i posted the only rule i could think that i broke was the "only a small snippet" but i figured since i highlighted the code i was working on that sufficed. I figured putting the rest of the code in there would show my effort that i put into this program, so you could see i wasnt looking for someone to do my homework. Sorry for whatever rule i broke.
Anyway thanks for your reply, but it seems whatever string i use to do a .find it returns an error and doesnt seem to work, i used the userChoice to store whatever the user inputed, so i was curious how tempString would work to find search text? thank in advance for the help!
Last edited by Dvb701 : Nov 20th, 2007 at 3:01 am.
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: Dvb701 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Dvb701 Dvb701 is offline Offline
Newbie Poster

Re: taking user input and searching a .txt file for matching records need a hint please

  #4  
Nov 20th, 2007
I guess i just dont really understand the whole search thing, if you didnt already notice this is a phone book program, and i need it to be able to search for and display results that the user inputs and display how many matches it found
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: taking user input and searching a .txt file for matching records need a hint plea

  #5  
Nov 20th, 2007
Originally Posted by Dvb701 View Post
I figured putting the rest of the code in there would show my effort that i put into this program, so you could see i wasnt looking for someone to do my homework.
Sure it shows your effort, but there's no indentation that makes the code readable.
Justlikeinenglishifyoudontusespacingcorrectlyyoucanteasilyreadthesentence.

Originally Posted by Dvb701 View Post
...but it seems whatever string i use to do a .find it returns an error and doesnt seem to work, i used the userChoice to store whatever the user inputed, so i was curious how tempString would work to find search text? thank in advance for the help!

Since we have no idea what you tried, no idea what was in the variables, no clue what the error was, what would you like me to tell you?
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
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 1:53 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC