Read and select a data from a file

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


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<cstring>
using namespace std;
int main()
{
string name;
int rn;
double ma,ne,prf;

ifstream show("Africa.txt");


cout<<"Enter the Registered ID :";
cin>>rn;

 while(show.eof())
{ // show>>rn>>name>>ma>>ne>>prf;

   cout<<"Your profit is "<<prf ;

show>>rn>>name>>ma>>ne>>prf;


}

show.close();


return 0;
}
closed account (2b5z8vqX)
when I input rn,i dont get any output..


while(show.eof())

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.

http://en.cppreference.com/w/cpp/io/basic_ios/eof
Last edited on
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;
} 
Last edited on
Thanks..
But,It outputs all the prifits.I want print only the selected one.
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.
I trird this but nothing changed,it gives the same output..
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
string name;
int rn;
double ma,ne,prf;

ifstream show("Africa.txt");


cout<<"Enter the Registered ID :";
cin>>rn;

while(!show.eof())
{//  show>>rn>>name>>ma>>ne>>prf;

   if(rn==1)
 {
 cout<<"urs "<<prf<<endl;
  }

    if(rn==2)
 {
 cout<<"urs "<<prf<<endl;
  }

  if(rn==3)
 {
 cout<<"urs "<<prf<<endl;
  }

  if(rn==4)
 {
 cout<<"urs "<<prf<<endl;
  }

  if(rn==5)
 {
 cout<<"urs "<<prf<<endl;
  }
show>>rn>>name>>ma>>ne>>prf;
}

show.close();


return 0;
}
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;
        }

    }



Thanks.. it works...
thanks for your time... !
Topic archived. No new replies allowed.