Question
Instructions Write a program that allows the user to search through a list of names stored in a file for a particular one. (Make sure
Instructions
Write a program that allows the user to search through a list of names stored in a file for a particular one. (Make sure to allow the user to tell you the name of their file and to check for file open errors.) The names in the file will be stored one per line (spaces are allowed in the names). You won't know the length of the list of names ahead of time.
Your search must be case insensitive.
Make sure to adequetly stest your program! (Empty data set, find first item, find last item, find a few in the middle, etc.)
Don't forget to read the file's name from the user and protect your program against any errors that may occur during the opening of the file.
Bob Smith
Mary Jones
Tammy Henry
Rob Smith
and a sample run might look like:
Please enter the name of your names file: bob.dat
I'm sorry, I could not open 'bob.dat'.
Please enter another name: names
File 'names' opened successfully!
What name would you like to find in this file? tammy henry
'Tammy Henry' is the 3rd name in the file!
Don't forget about when the name isn't in the file.
#include
using namespace std;
int main() { string names = ""; string filename, name; cout<<"Please enter the name of your names file"; cin>>filename; ifstream file; while(!file.is_open()){ file.open(filename); if(!file){ cout<<"I'm sorry, I couldn not open'"<
cout<< "What name would you like to find in this file? "; getline(cin, name); transform (name.begin(), name.end(), name.begin (), ::tolower);
string line; int line_number =0, found_at = -1; while(getline(file,line)) { line_number++; transform(line.begin(),line.end(), line.begin(), ::tolower); if(line.find(name) != string::npos) { line_number++; break; } } if(found_at != -1) { cout<< "'" << line << " 'is the " << found_at << "the name in the file! "; } else { cout << "Name not found in file. "; }
file.close(); return 0;
}
I just want to know when I should write the names, and what is wrong with code??
ASAP PLS!!!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started