Hello guyz!!! Please help me to search a records in text file.
The text file written of this records:

Ben 2010-0001 300.00
John 2010-0003 400.00
Mike 2010-0002 190.00

I want to input only the employee id and it will display the name and salary if that record is existed.

Here's my code:

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

using namespace std;

int main()
{

	string Ename;
	int emp_rec, no, emp;
	string cname,
	int emprec, no_na;

	ifstream EmpRec;
	ofstream DTR;
	
	EmpRec.open("employee.txt");

	if (!EmpRec)
	{
		cout<<"Cannot open input file"<<endl;
		cout<<"Program Terminated"<<endl;
		return 1;
	}



	EmpRec>>Ename>>emp_rec>>no;

	while(EmpRec)
	{
		cout<<"Enter Employee record: ";
		cin >>emp;

		if (emp_rec==emp){
		cname=Ename;
		emprec=emp_rec;
		no_na=no;
		}
	}

	cout<<cname<<endl;
	cout<<emprec<<endl;
	cout<<no_na;

	return 0;
	
}

Tnx : )

Firstly, please learn to use code tags

EmpRec>>Ename>>emp_rec>>no;

This is reading exactly one line from your text file and stopping. The expression you have in your while loop condition isn't actually doing anytihng. Put mpRec>>Ename as your while loop condition

while(EmpRec >>Ename)
{
      EmpRec >> emp_rec>>no; //you might be able to jam all 
             //this into the loop condition but 
             //it's a bit easier to read
}

Prompt the user for input before you go into the while loop, the way you have it the user can keep entering a new number as each record is read.
If your data you are checking is of the form 2010-0001 how do you suppose your int is going to hold the dash? Much better to make it a string.
I'm sure there'll be other things that come up, but this is something to go on for now.

cout<<"Enter Employee record: ";
		cin >>emp;
 
	while(EmpRec)

	{
			EmpRec>>Ename>>emp_rec>>no;
		
 
		if (emp_rec==emp){
		cname=Ename;
		emprec=emp_rec;
		no_na=no;
		}
	}

This is what i did sir..but i am not comfortable with the result..
can I use this one,

string emp[10];
//10 is no. of rows
for(i=0; i<10;i++)
EmpRec>>emp[i];

emp arrays is will hold the data

commented: My mistake. That's 5 times you've been asked. Infraction is next. -2

I'm not going to help you any further until you use the code tags(use the edit button to edit your post). Use them as follows:

[code]

#include ... etc.

[/code]
Or highlight your code and hit the

button in the menu bar of the editing area

EDIT: You were already over the time limit I think.[code]
button in the menu bar of the editing area

EDIT: You were already over the time limit I think.

can I use this one,

Only if you know exactly how many records there are. You can't just use the ifstream object alone as your loop condition the way you have it.
Try it with the arrays and post back (with code tags as my provision still stands).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.