Question: A student has this code below in c++ that is suppose to allow the user to search for a .txtfile by searching by fname, lname,number.
A student has this code below in c++ that is suppose to allow the user to search for a .txtfile by searching by fname, lname,number. etc and read the .txtfile. Can you figure out what is wrong with the code and show your output. You can use your own.txtfile.
#include
#include
#include
#include
using namespace std;
char Fname[2000];
char filename[1000]; char data
[10000]; char file;
string Lname[56];
string number; string Email;
char word; string document;
string fname;
string lname;
int ch=0;
struct menu{
int menu1=0;
char repeat='Y'/'N';
string fin;
string y;
string search;
string enter;
string line;
int number;
string firstName[1000];
string lastName[500];
string email[500];
char menu;
};
class menuclass{
public:
void menu();
void display();
void displayall(){
for(int i =0; i<1; i++){
cout<<"First name:"<
cout<<"Last name:"<
cout<<"email:"<
}
}
// Function to perform loading the data of file into arrays
void loadFile(){
fstream file;
cout<<"Enter the name of the file";
cin>>filename;
cin.ignore();
file.open(filename,ios::out|ios::in|ios::app);
ifstream infile;
string line;//for read line
cout<<"Loading the file........"<< endl;
//open the below file
infile.open ("myList.txt");
int counter=0;
if (infile.is_open()){ //if file opened
while( getline(infile, line,' ') ) { //get row from text file
stringstream ss(word); //stream to other variable
ss>>Fname[counter]>>Lname[counter]>>Email[counter];
counter++;
}
infile>>data;
while(!infile.eof()) {
cout << data << endl;
infile>>data;
}
// close the opened file.
infile.close();
infile.close(); //close file
cout<<"File scan done........"<< endl;
}
else{ //if file not found show the below message
cout << "Sorry, we could not find the file." << endl;
}
}
// Function to perform search based on first name
void searchByFirstName(string fname){
// Function to perform search based on first name
for(int i =0; i<1; i++){
if(ch==Fname[i]){
cout<<"**Found The following file .txtfile name was found ";
cout <<"Display "<
}
else{
cout<<"Not found";
}
}
}
// Function to perform search based on first name
void searchByLastName(string lname){
// Function to perform search based on first name
for(int i =0; i<500; i++){
if(ch==lname[i]){
cout<<"**Found The following details with matching last name** ";
cout<<"First name:"<
cout<<"Last name:"<
cout<<"email:"<
else{
cout<<"Not found"
}
}
}
// Function to perform search based on first name
void searchByNumber(int number){
number = number-1; // get actual array index
// Check for row number validity
if(number<= 500 && number >0){
cout<<"**Found The following details ** ";
cout<<"First name:"<
cout<<"Last name:"<
cout<<"email:"<
}
else{
cout<<"Invalid number";
}
}
};
int main (){
int menu1=0;
char repeat='Y'/'N';
string y;
int done=0;
string search;
string enter;
string line;
int number;
//Create object of menuclass
menuclass menuclass ;
do{
cout << "Please pick one of the following Choices" << endl;
cout << "1)" << " pepsi " << endl;
cout << "2)" << "dr.pepper" << endl;
cout << "3)" << "orange crush " << endl;
cout << "4)" << "lemonade" << endl;
cout << "5)" << "water " << endl;
cin >> menu1;
// Load the file data
menuclass.loadFile();
switch (menu1){
case 1:
cout << "displayall"<< endl
// call the displayall function
menuclass.displayall();
break;
case 2:
cout << "Search by first name" << endl;
cin >> enter;
// call the searchByFirstName function
menuclass.searchByFirstName(enter);
break;
break;
case 8:
cout << "Exit"<< endl;
break;
default:
cout <<"not valid"<< endl;
for( menu1 = 0; menu1 <1; menu1++ )
getline(cin,enter); }
//calculator stuff, better to be in a separate function
cout << " Do you want to continue (Y/N)? ";
cout << "You must type a 'Y' or an 'N' :";
cin >> repeat;
}
while ((repeat == 'Y') || (repeat == 'y'));
cout<<"*exit _ "; return 0;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
