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
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');
I am working at my programming project for school and i came up with idea of "Student Manager Program".
The: name, roll, year, group, course, adress, email, contact, grade are stored as variables in class named Student.
Student's data is written by user keyboard input and saved in studentRecord.txt file, by object.
How can i prevent this while loop to be a not infinite loop. I can't find solution to this problem.
'''
//Displaying Students Record Table
void Student::display() {
system("cls");
ifstream file;
int total = 1;
int x;
cout << "\n-----------------------------Students Record Table-----------------------------" << endl;
file.open("studentRecord.txt");
if (!file) {
cout << "\n\t\t\tNo Data Is Present.";
file.close();
}
else {
file >> name >> roll >> year >> group >> course >> adress >> email >> contact >> grade;
while(!file.eof()) {
cout << "\n\n\t\t\t Student Consecutive Number: " << total++ << endl;
cout << "\t\t\t Student's Name: " << name << endl;
cout << "\t\t\t Student's ID Number: " << roll << endl;
cout << "\t\t\t Student's Year: " << year << endl;
cout << "\t\t\t Student's Group: " << group << endl;
cout << "\t\t\t Student's Course: " << course << endl;
cout << "\t\t\t Student's Adress: " << adress << endl;
cout << "\t\t\t Student's Email: " << email << endl;
cout << "\t\t\t Student's Contact: " << contact << endl;
cout << "\t\t\t Student's Final Grade: " << grade << endl;
file >> name >> roll >> year >> group >> course >> adress >> email >> contact >> grade;
}
if (total == 0) {
cout << "\n\t\t\tNo Data Is Present.";
}
}
do {
cout << "\n\t\t\t If You Want to Back at previous page press 0 and Enter: ";
cin >> x;
if (x == 0) {
file.close();
}
} while (x != 0);
}
'''
Struggling really hard with this, I need to enter an ID number thats in a text file and output the associated grades that are on the same line of the text file
This is the code I have, it prints the correct average but it prints the test scores of the next ID numeber
int searchID;
int studentID;
bool found = false;
double exam_1;
double exam_2;
double exam_3;
double average = 0;
cout << "Enter student ID to search in file " <<
fileName << " : ";
//read searchID value
cin >> searchID;
//read data from file until the search id is found
while (fin >> studentID >> exam_1 >> exam_2
>> exam_3 && !found)
{
if (searchID == studentID)
{
average = (exam_1 + exam_2 + exam_3) / 3.0;
found = true;
}
}
//close the file stream,fin
fin.close();
//check if search id is found
if (found)
{
cout << left << setw(10) << "Exam 1" << setw(10) << exam_1 << endl;
cout << left << setw(10) << "Exam 2" << setw(10) << exam_2 << endl;
cout << left << setw(10) << "Exam 3" << setw(10) << exam_3 << endl;
cout << fixed << setprecision(2)
<< "Average : " << average << endl;
}
else
cout << searchID << " student id is not found." << endl;
system("pause");
Your got the loop condition wrong. Lets say one iteration found the ID, found is set to true, then for the next iteration the condition is evaluated:
while (fin >> studentID >> exam_1 >> exam_2 >> exam_3 && !found)
No matter what is the value of found you read the next entry in the file. Change it to
while (!found && fin >> studentID >> exam_1 >> exam_2 >> exam_3)
&& short-circuits, ie when !found is false, the rest of the condition is not evaluated.
I am writing a code that takes some input from the user and stores it in a file. My code is supposed to keep the old data and add the new data, but every time I run the code the old data that was in the file gets replaced by the new one.
if(input == 1){
outFile.open("personnel2.dat");
int numRecords = 0;
do{
cout << "#1 of 7 - Enter Employee Worker ID Code(i.e AF123): ";
cin >> id;
cout << "#2 of 7 - Enter Employee LAST Name: ";
cin >> lastN;
cout << "#3 of 7 - Enter Employee FIRST Name: ";
cin >> firstN;
cout << "#4 of 7 - Enter Employee Work Hours: ";
cin >> workH;
cout << "#5 of 7 - Enter Employee Pay Rate: ";
cin >> payRate;
cout << "#6 of 7 - Enter FEDERAL Tax Rate: ";
cin >> federalTax;
cout << "#7 of 7 - Enter STATE Tax Rate: ";
cin >> stateTax;
outFile << id << " " << lastN << " " << firstN << " " << workH << " " << payRate << " "
<< federalTax << " " << stateTax << "\n";
numRecords++;
cout << "Enter ANOTHER Personnel records? (Y/N): ";
cin >> moreRecords;
}while(moreRecords != 'N' && moreRecords != 'n');
outFile.close();
cout << numRecords << " records written to the data file.\n";
}
Change outFile.open("personnel2.dat");
to
outFile.open("personnel2.dat", std::fstream::app);
to set the mode to append, assuming you are using fstream::open().
Assuming that outfile is instance of std::ofstream, The reason behind that is that when you use open() function on ofstream object, the file is opened in ios_base::out mode which enforces removal of previous content before inserting new one.
In order to append the data, you have to explicitly specify the append mode.
Example:
#include <fstream> // std::ofstream
int main () {
std::ofstream ofs;
ofs.open ("test.txt", std::ofstream::out | std::ofstream::app);
ofs << " more lorem ipsum";
ofs.close();
return 0;
}
Source: http://www.cplusplus.com/reference/fstream/ofstream/open/
In your case, you have to change it in this way:
outFile.open("personnel2.dat", std::ofstream::out | std::ofstream::app);
I'm currently taking an online C++ class and our current project requires us to read a file, take each student and put it into a vector.
My current problem is splitting the name read and setting it to the right variables.
The professor's pseudo code here:
inputFile.open(sFileName.c_str ());
while(inputFile.fail())
{
cout ERROR OPENING FILE
cout PLEASE REENTER THE PASSWORD OF THE FILE
getline(cin >> wd, sFileName);
inputFile.open(sFileName.c_str());
}
inputFile.clear();
inputFile.seek(0, ios::beg);
while(getline(inputFile, sTemp))
{
istringstream inputSStream(sTemp)
inputSStream >> sFirstName >> sMiddleName >> sLastName >> sID >> sClass;
if(sMiddleName != "|")
sFullName = sFirst name + " " + sMiddleName +" " + sLastName;
else
sFullName = sFirstName + " " + sLastName;
My current code here:
ifstream myFile;
string firstName, middleName, lastName, fullName, studentID, cID;
string inFileName, stringTemp;
cout << "Please enter the name of the file that you want to read in. \n";
cin >> inFileName;
myFile.open(inFileName);
while (myFile.fail())
{
cout << "\n""";
cout << "Error file, Please re-enter file. \n ";
cin >> inFileName;
myFile.open(inFileName);
}
myFile.clear();
myFile.seekg(0, ios::beg);
while (getline(myFile, stringTemp))
{
istringstream inputStream(stringTemp);
inputStream >> firstName >> middleName >> lastName >> studentID >> cID;
if (middleName != "|")
fullName = firstName + " " + middleName + " " + lastName;
else
fullName = firstName + " " + lastName;
//Checking what the input values are
cout << "First name is " << firstName << endl;
cout << "Middle name name is " << middleName << endl;
cout << "Last name is " << lastName << endl;
cout << "studentID is " << studentID << endl;
cout << "CID is " << cID << endl;
Student thisStudent(fullName, studentID, cID);
studentList.push_back(thisStudent);
}
myFile.close();
The current problem is that the line isn't split correctly, first name gets the whole line and mid and last name, studentID and cID all remain empty.
Here is the current file that I'm reading in:
ERIC,ANTHONY,TURNER,1234573,CISC_198
GABRIEL,FEIJO,LOPES,1234574,CISC_199
GEOFFERY,BRYAN,RANSOM,1234575,CISC_200
HANNAH,MAE ,LONGRIE,1234576,CISC_201
HASSAN,ISMAIL,AHMED,1234577,CISC_202
HUNG,B,PHAM,1234578,CISC_203
HUSSEIN,FOUAD,ALJANABI,1234579,CISC_204
JING,XUN,CHEN,1234580,CISC_205
KAJALBEN,CHIMANLAL,MAKWANA,1234581,CISC_206
DANDREA,SHAMAICAH,BUSH,1234570,CISC_195
DANIELLE,MARIE,CORTEZ,1234571,CISC_196
ERDI,T,KIDANE,1234572,CISC_197
AARON,FABIAN,LINGAD,1234567,CISC_192
AARON,T,PATCHIN,1234568,CISC_193
ALI,FOUAD,ALJANABI,1234569,CISC_194
NATHAN,|,NANN,1234585,CISC_210
NEIL,ANDREAS,FRANKA,1234586,CISC_211
OBONE,|,ORIYAVONG,1234587,CISC_212
OLIVIA,JOANNE,MAILANDER,1234588,CISC_213
RALEIGH,|,COSGROVE,1234589,CISC_214
RYAN,PAREDES,PALMARES,1234590,CISC_215
MICHAEL,DUONG,NGUYEN,1234583,CISC_208
MIGLENA,|,CHEMELEKOVA,1234584,CISC_209
STEPHEN,MICHAEL,HOUSE,1234591,CISC_216
MARCUS,D,BUTLER,1234582,CISC_207
Here is the output that I do not understand how to fix.
First name is ERIC,ANTHONY,TURNER,1234573,CISC_198
Middle name name is
Last name is
studentID is
CID is
When you read with operator >> you are splitting on whitespace. Despite the name, getline() can split by other characters than newline:
getline(inputStream, firstName, ',');
getline(inputStream, middleName, ',');
// ...