•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,964 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 3,607 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: 809 | Replies: 4
![]() |
| |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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.
Re: taking user input and searching a .txt file for matching records need a hint plea
#2
Nov 20th, 2007
•
•
•
•
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()) Age is unimportant -- except in cheese
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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!
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.
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Re: taking user input and searching a .txt file for matching records need a hint please
#4
Nov 20th, 2007
Re: taking user input and searching a .txt file for matching records need a hint plea
#5
Nov 20th, 2007
•
•
•
•
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.
Justlikeinenglishifyoudontusespacingcorrectlyyoucanteasilyreadthesentence.
•
•
•
•
...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?
Age is unimportant -- except in cheese
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- how do I have txt file be read by the compiler? (C++)
- Setting an array and reading in a txt file backwards (C++)
- Password Protection from a txt.file (HTML and CSS)
- Error Checking for user input (Java)
- Help with comparing user input to a text file! (C++)
- uploading .txt file via phpmyadmin to Mysql (MySQL)
Other Threads in the C++ Forum
- Previous Thread: File Open dialog created using GetOpenFileName function does not close
- Next Thread: infile.peek function



Hybrid Mode