0

I am going to read a file and output all the elements. But when I am reading and outputting, It will just display the first element only and won't go through the second. Thank you guys first. Appreciate

int main(){
    ifstream patientRead ;
    patientRead.open("Patient_List.txt") ;
    string name ,ID, address , docName , diagnosis , patientStatus , consultationDate ;
    int age ;

    if(patientRead.is_open()){
        while(getline(patientRead ,name), patientRead >> ID >> age >> address >> docName >> diagnosis >> patientStatus >> consultationDate){

            int count = 0;
            count ++ ;
            cout<< "Patient "             << count             << endl ;
            cout<< "Name              : " << name              << endl ;
            cout<< "ID                : " << ID                << endl ;
            cout<< "Address           : " << address           << endl ;
            cout<< "Doctor Name       : " << docName           << endl ;
            cout<< "Diagnosis         : " << diagnosis         << endl ;
            cout<< "Patient Status    : " << patientStatus     << endl ;
            cout<< "Consultation Date : " << consultationDate  << endl << endl;
        }        
    }else{
        cout << "Unable to open" ;
    }
    patientRead.close();
}

This is the pattern of the file(File Name: "Patient_List.txt"):

ZI ZI ZI
UMMY123456 19 NO1234 DrDomo FEVER OUTPATIENT 15/5/2021
LIM LIM LIM
UMMY987654 25 NO0987 DrDomo FEVER OUTPATIENT 15/5/2021
domo
  • 1
  • 1
  • 1
    You probably want to declare `count` outside the loop. – molbdnilo Jul 08 '21 at 14:04
  • 3
    Mixing `getline` and `>>` is tricky. https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – Retired Ninja Jul 08 '21 at 14:04
  • I want count +1 every time and display out – domo Jul 08 '21 at 14:05
  • Which is why you want to declare it before the loop. Otherwise the count will be 1 every time. – drescherjm Jul 08 '21 at 14:06
  • If dont want mix it up, how can I do ?declare name1, name2, name3 and then Patient >> name1 >> name2 >> name3 , Like this? But I tried it before still cant display the LIM LIM LIM – domo Jul 08 '21 at 14:08
  • You need to read @RetiredNinja's link. (It may not look like it's the same, but you have `>>` last in one iteration, and `getline` first in the next.) – molbdnilo Jul 08 '21 at 14:09
  • Oh god. Thank You for helping me. It works with adding a .get() . Appreciate. – domo Jul 08 '21 at 14:36

1 Answers1

1

This is easy if you let the object read in its members:

struct Patient
{
    string name;
    string id;
    string address;
    string docName;
    string diagnosis;
    string patientStatus;
    string consultationDate;

    friend std::istream& operator>>(std::istream& input, Patient& p;
};

std::istream& operator>>(std::istream& input, Patient& p)
{
    std::getline(input, p.name);
    input >> p.id;
    input >> p.address;
    input >> p.docName;
    input >> p.diagnosis;
    input >> p.patientStatus;
    input >> p.consultationDate;
    input.ignore(1000, '\n');
    return input;
}

The input loop would look like:

Patient p;
std::vector<Patient> database;
while (patientRead >> p)
{
    database.push_back(p);
}

The getline in the input method reads in the patient name.
The input.ignore() synchronizes the input to so the patient name can be read.

If you are allergic to structures and methods, you could copy and edit the content of the operator>> method:

std::getline(patientRead, name);
patientRead >> id;
patientRead >> address;
patientRead >> docName;
patientRead >> diagnosis;
patientRead >> patientStatus;
patientRead >> consultationDate;
patientRead.ignore(1000, '\n');
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154