Reading certain line instead of all

Hi guys,how do i implement codes where it shall prompt for an user id.

From the input,the system shall look into the file..and output if the data exist.

For example in the file,

A123
Robinson
012345

B345
Crusoe
543210

So,i juz wanna input for B345.
Output shall be:
B345
Crusoe
543210


The code below is reading the whole data in the file but I only want to read one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{
	fstream inFile;
	string id,name,hp_no;
	char tmp[30];
        cout<<"Please enter the id that you would like to search for:\n";
        getline(cin,id);//currently stucked here...
	inFile.open("test.txt",ios::in);
	if(!inFile)
		cout<<"Error opening file...\n";
	while(!inFile.eof())
	{
		inFile>>id;
		cout<<"ID:"<<id<<endl;
		inFile.getline(tmp,30);
		getline(inFile,name);
		cout<<"Name:"<<name<<endl;
		inFile>>hp_no;
		cout<<"Hp No:"<<hp_no<<endl;
		cout<<endl;
	}
	system("pause");

	return 0;
}
I think you need in the while loop a conditional statement that compares the line you are getting from a file and a desired data( in your case is B345). Once you got that, print the next two lines and break the while loop.

Pseudo Code:
while ( reading from a file )
{
temp variable = get line from the file;
if ( desired data == temp )
{
print the desired data and the next two lines;
break;
}
}
Last edited on
Hi.

thx for ur reply..now my code is as following.

But now my new question is,where/how i implement code to
cout<<"Sorry,ID not found in the system.\n";\\if input doesnt match any data in file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main()
{
	fstream inFile;
	string name,id,idchk,hp_no;
	char tmp[50];
	cout<<"Enter id:\n";
	cin>>id;
	inFile.open("test.txt",ios::in);
	if(inFile.good())
	{
		do
		{
			inFile>>idchk;//finalised	
			inFile.getline(tmp,50);//finalised
			getline(inFile,name);//finalised
			inFile>>hp_no;//finalised
		}
		while(id!=idchk);
		inFile.close();
	}
	else
		cout<<"File does not exist.\n";
		cout<<"Student ID:"<<id<<endl;
		cout<<"Student Name: "<<name<<endl;
		cout<<"Student Contact No:"<<hp_no<<endl;

		system("pause");
		return 0;
	}
	
You might want to do something like :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char *buffer[3];
bool id_found;
while(infile >> buffer[0] >> buffer[1] >>  buffer[2])
{
    if(buffer[1] == id)
    {
         cout << data << data << endl;
         id_found = true;
    }
    else
    {
          cout << "parsing next file" << endl;
    }
}
if(id_found != true)
{
    cout << "Sorry, ID not found in the system" << endl;
}
Last edited on
thx for replying.

is there any other way to juz add the statement into my current program codes? abit new to file & string chapters...and dun really understand ur codes ><
Edit : Comment wasn't very constructive.

Look through the tutorials about data types, if else statements, and loops. These topics contain all the information you need to make your code work.
Last edited on
If you don't understand my code you might want to go back through some of the tutorials on this site.

Considering your code hoses random memory and you think comparing pointers is useful in this situation, perhaps he's not the only one that should be reviewing some material. =)
I would suggest storing the data in a container when getting it from the file, then manipulating the information int the container you have chosen, once done you just simply over-write the file with the

to keep it simple you can use a vector you know that all the data is stored as 3s so when you get a match you simply over write the data for the next two elements with the new.

else you can have a class and use even a map if you know how to. lots of flexible ways to do it. just go with the one that is easier for you.
Topic archived. No new replies allowed.