By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,347 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,347 IT Pros & Developers. It's quick & easy.

Reading several records from sequential files

P: 1
My program is up and running with no errors whatsoever the problem it is only displaying a single record from the text file instead of all the records that i have done...
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <fstream.h>
  3.  
  4. #include <string.h>
  5.  
  6.  
  7.  
  8. class Patient
  9. {
  10. public:
  11.     char name[15];
  12.     char DOB[10];
  13.     char sex[6];
  14.     char residence[10];
  15.  
  16. public:
  17.  
  18.         int record(int number)
  19.         {
  20.             return number;
  21.  
  22.         }
  23.  
  24.  
  25.  
  26.         void setName(char *patName)
  27.         {
  28.             strcpy(name,patName);
  29.         }
  30.  
  31.  
  32.  
  33.         void setDOB(char *patDOB)
  34.         {
  35.             strcpy(DOB,patDOB);
  36.         }
  37.  
  38.         void setGender(char *patGender)
  39.         {
  40.             strcpy(sex,patGender);
  41.         }
  42.  
  43.         void setResidence(char *patRes)
  44.         {
  45.             strcpy(residence,patRes);
  46.         }
  47.  
  48.         double fee(double cash)
  49.         {
  50.             return cash;
  51.         }
  52.     };
  53.  
  54.  
  55.  
  56.  
  57.     int main()
  58.     {
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.         int number;
  67.         char name[15];
  68.         char DOB[10];
  69.         char sex[6];
  70.         char residence[10];
  71.         double cash;
  72.  
  73.  
  74.  
  75.  
  76.  
  77.     /*    char sname[15];
  78.         cout<<"Enter the name of the patient:"<<endl;
  79.         cin>>sname;
  80.  
  81.         ifstream myfile ("patient.txt", ios::in );
  82.         if (myfile.is_open())
  83.         {
  84.             while (!myfile.eof())
  85.             {
  86.  
  87.  
  88.                 if (sname==name){
  89.                     cout<<"This is the file you searched!"<<endl;
  90.                 myfile>>number>>name>>DOB>>sex>>residence;
  91.                 }
  92.                 else {
  93.                     cout<<name;
  94.                 }
  95.  
  96.             }    
  97.  
  98.  
  99.  
  100.         myfile.close();
  101.         }
  102.         else cout << "Unable to open file";*/
  103.  
  104.  
  105.         while (1){
  106.  
  107.  
  108.  
  109.  
  110.         cout<<"_______________________"<<endl;
  111.         cout<<"Enter patient Number: "<<endl;
  112.         cin>>number;
  113.         cout<<"Patient name:"<<endl;
  114.         cin>>name;
  115.         cout<<"DOB"<<endl;
  116.         cin>>DOB;
  117.         cout<<"Gender:"<<endl;
  118.         cin>>sex;
  119.         cout<<"residence:"<<endl;
  120.         cin>>residence;
  121.         cout<<"fee"<<endl;
  122.         cin>>cash;
  123.  
  124.  
  125.         Patient myPatient;
  126.         myPatient.record(number);
  127.         myPatient.setName(name);
  128.         myPatient.setDOB(DOB);
  129.         myPatient.setGender(sex);
  130.         myPatient.setResidence(residence);
  131.         myPatient.fee(cash);
  132.         cout<<"_______________________"<<endl;
  133.         cout<<"Patient Number: "<<myPatient.record(number)<<endl;
  134.         cout<<"Patient Name: "<<myPatient.name<<endl;
  135.         cout<<"Patient DOB: "<<myPatient.DOB<<endl;
  136.         cout<<"Patient Gender: "<<myPatient.sex<<endl;
  137.         cout<<"Patient Residence: "<<myPatient.residence<<endl;
  138.         cout<<"Consultation fee: "<<myPatient.fee(cash)<<endl;
  139.  
  140.         ofstream mfile ("patient.txt", ios::out | ios::app );
  141.         if (mfile.is_open())
  142.         {
  143.  
  144.         mfile << myPatient.record(number)<<endl<<myPatient.name<<endl<<myPatient.DOB<<endl<<myPatient.sex<<endl<<myPatient.residence<<endl
  145.             <<myPatient.fee(cash)<<endl;
  146.         mfile.close();
  147.         }
  148.  
  149.         else {cout << "Unable to open file";}
  150.  
  151.  
  152.         cout<<"Reading from file"<<endl;
  153.  
  154.  
  155.         ifstream infile("patient.txt"); 
  156.         if(infile.is_open())
  157.         {
  158.         infile >> number; 
  159.         cout << number << endl; 
  160.         infile >> myPatient.name; 
  161.         cout << name << endl; 
  162.         infile >> myPatient.DOB;
  163.         cout << DOB << endl;
  164.         infile >> myPatient.sex;
  165.         cout << sex<<endl;
  166.         infile >> myPatient.residence;
  167.         cout << residence<<endl;
  168.  
  169.         infile.close();
  170.  
  171.         }
  172.         else {
  173.  
  174.         cout<<"Unable to open file: "<<endl;}
  175.  
  176.     }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.         return 0;
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
Nov 11 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
you did not have a loop to read sucessive records, it should be something like
Expand|Select|Wrap|Line Numbers
  1.         ifstream infile("patient.txt"); 
  2.         if(infile.is_open())
  3.             {        
  4.             while(infile.good())  // ** read while file is good()
  5.               {
  6.               double fee;  // ** added
  7.               infile >> number; 
  8.                     if(!infile.good())break;  // ** if first read fails break out of loop
  9.                 cout << number << endl; 
  10.                 infile >> myPatient.name; 
  11.                 cout << name << endl; 
  12.                 infile >> myPatient.DOB;
  13.                 cout << DOB << endl;
  14.                 infile >> myPatient.sex;
  15.                 cout << sex<<endl;
  16.                 infile >> myPatient.residence;
  17.                  cout << residence<<endl;
  18.                 infile >> fee;  // ** added
  19.                 cout << fee << endl << endl;  // ** added
  20.              }
  21.         infile.close(); // **moved outside loop
  22.        }
  23.         else {
  24.  
  25.         cout<<"Unable to open file: "<<endl;}
  26.  
Nov 11 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.