when I input rn,i dont get any output..
in Africa.txt i have saved
1 Su 1000 1203 20.3
2 Qu 3904 4345 11.2961
3 Anshu 6895 6905 0.145033
4 Sahoo 5644 5676 0.566974
5 Mithuri 6000 7654 27.5667
This is because the condition of the while loop immediately evalautes to false, and thus, execution of its compound statement never occurs. You likely meant to write !show.eof() to reiterate until true is returned.
In any case, it's not a good idea to test for eof() in a while loop. Instead, put the actual input statement in the condition. Then the body of the loop is executed only after a successful input was read.
1 2 3 4 5 6 7 8 9 10 11 12
while (show>>rn>>name>>ma>>ne>>prf)
{
// whatever you want here
cout << "rn: " << rn
<< " name: " << name
<< " ma: " << ma
<< " ne: " << ne
<< " prf: " << prf
<< endl;
cout<<"Your profit is "<<prf << endl;
}
Well then, you need to add a test, use an if statement to check for the required details. I only showed how to read the file, I assumed you could at least have a go at the rest.
Note that at present the variable rn is used to get the user input, and then the same variable is used to read from the file, thus the user input is lost. You will need a separate variable to keep track of both pieces of information.
Give it a try, if you get stuck, post your updated code so we can see what the problem is.
As mentioned previously, the value for rn entered by the user will be replaced by the one which is read from the file. you need to use two completely separate variables for this.
Also within the while loop, on the first pass, the file has not yet been accessed, so the various data items contain uninitialised (garbage) values. It is better to not use eof() in the loop condition. Instead put the file input code there. This way, the body of the loop will be entered only after a successful input operation.
1 2 3 4 5
while (show>>rn>>name>>ma>>ne>>prf)
{
// we get to here only after the file input was successful
// put whatever code is needed here...
}
Now within the loop, all of your if statements perform the same action, so there is no way to tell which one was executed. But I don't think this is what you really need. I presume (am I wrong?) that you want to take the value of Registered ID which was entered by the user and compare that with the value which has been read from the file. If that's the case, then your code might look something like this:
1 2 3 4 5 6 7 8 9
while (show>>file_rn>>name>>ma>>ne>>prf)
{
if ( user_rn == file_rn )
{
cout<<"urs "<<prf<<endl;
}
}