Searching and Displaying a specific line

// hi i have a problem in option no. 3 because my code doesn't read a specific //or the text i input and it also not displaying the line in command prompt. //what is the problem here in my source code?

#include <fstream>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <limits>
using namespace std;
int main()
{
int start;
int i = 1;
int counter = 0;
int choice2;
string str1;
string str2;
string str3;
string str[2];
ifstream file("policerec.txt");

menu:
cout << " 1) Add a new entry to Police Record" << endl;
cout << " 2) View Police Record" << endl;
cout << " 3) Search A Police Record Entry" << endl;
cout << " 4) Delete A Police Record Entry" << endl;
cout << " \n\nEnter The No. of your Choice: ";
cin >> start;
file.close();

{
if (start == 1)
{
cout << "\n\n---------------------------------------\n\n";
cout << "Enter First Name: ";
cin >> str1;
cout << "Enter Last Name: ";
cin >> str2;
cout << "Enter Address: ";
cin >> str3;
ofstream file_("policerec.txt");
if (file_.is_open())
file_ << str1 << " ";
file_ << str2 << " ";
file_ << str3 << "\n";
std::cin.get();
file.close();
cout << "\nEntry succesfully added in Police Record\n";
finish:
cout << "\n1. Go back to Main Menu";
cout << "\n2. Exit the Program";
cout << "\n\nChoice: ";
cin >> choice2;
{
if (choice2 == 1)
{
system("cls");
goto menu;
}
else if (choice2 == 2)
{
system("cls");
goto end;
}
else
{
system("cls");
cout << " Pick 1 or 2" << endl;
system("pause");
goto finish;
}
}
}
else if (start == 2)

{
ifstream file_("policerec.txt");
std::string firstname;
std::string lastname;
std::string address;
if (file_.is_open())
{
while (file_ >> firstname >> lastname >> address)
{
while (i < 2)
{
cout << "\n\n First Name: Last Name: Address:";
cout << "\n-------------------------------------------------\n\n";
i++;
}
cout << setw(15) << firstname;
cout << setw(15) << lastname;
cout << setw(15) << address << "\n";
}
file_.close();
}
else
std::cout << "\n\nFile is not open or No Record is Available" << "\n";

file.close();
cout << "\n1. Go back to Main Menu";
cout << "\n2. Exit the Program" << endl;
cout << "\nChoice: ";
cin >> choice2;
{
if (choice2 == 1)
{
system("cls");
goto menu;
}
else if (choice2 == 2)
{
system("cls");
goto end;
}
}
}
else if (start == 3){
{
std::ifstream file_("policerec.txt");
std::string searchname;
cout << "\n------------------------\n";
cout << "\n\nSearch by Name: ";
cin >> searchname;
std::string firstname;
std::string lastname;
std::string address;
std::string line;
if (file_.is_open())
{
{
while (!file.eof())
{
while (file_ >> firstname >> lastname >> address);
{
while (std::getline(file_, searchname));
{
if (searchname == firstname)
{
counter++;
std::cout << "\n\n" << "First Name: " << firstname;
std::cout << "\n" << "Last Name: " << lastname;
std::cout << "\n" << "Address: " << address << "\n";
system("pause>0");
}
}
}
}
}
}
else
std::cout << "\n\nFile is not open or No Record is Available" << "\n";

file.close();
cout << "\n1. Go back to Main Menu";
cout << "\n2. Exit the Program" << endl;
cout << "\nChoice: ";
cin >> choice2;
{
if (choice2 == 1)
{
system("cls");
goto menu;
}
else if (choice2 == 2)
{
system("cls");
goto end;
}
}
}
}
}
end:;
return 0;
}
Topic archived. No new replies allowed.