473,408 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Reading several records from sequential files

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
1 2980
horace1
1,510 Expert 1GB
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

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

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
2
by: satish | last post by:
Hi all, I am writing a file by name "input" out, with a small python code below: grid=open('input','w') grid.write(' ') grid.write('/*number_of_zone\n') grid.write(' ') grid.write('2\n')...
0
by: Chris Hall | last post by:
The records in my database are displayed in a form as follows: %> <form action="report-ammend1.42.asp" method="post"name="form"> <table border=1> <% x = 1
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
16
by: Roy | last post by:
I use a Access 2K application.I am trying to use Chuck Grimsby(clsReadTextFile.txt)class utility to read a text file and then do a import of the same into my database.The question is how to call...
3
by: Sudhesh | last post by:
Hi, I know its fairly easy in .NET to read a text file (fixed or delimited) using a Microsoft Text driver. My question is, is there a similar easy way to read text files if they have different...
2
by: Wes Peters | last post by:
Does anyone know of an article that deals with the subject of reading a structured text file using VBA code in Access? Thanks, Wes
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
4
by: ducttape | last post by:
Hi, I have been trying for several days to read XML files into my program. I have been following several good tutorials on the internet, but I am struggling because the particular XML files that I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.