Hello everyone !
I'm new here.
I'm trying to auto generate unique id for every patient who register in hospital management system.
So kindly help me with that.
Thank you so much.
Thanks JLBorges it helped me
But the problem is I need to apply this in File Handling where it should read the file and increment id by 1 each time when new patient is added.
Hi, umairbilal. If you can, speak by code: your question is not clear. JLBorges’s solution is a generic one: it fits nearly everywhere. Please, post a compilable example of what you’re trying to do to get better answers.
…in File Handling where it should read the file and increment id by 1 each time when new patient is added.
When you read a file, you do not add data; if you’re adding data, you’re writing into the file. Please explain.
And please specify if it is an assignment (I presume it is).
#include <string>
#include <fstream>
struct patient {
std::string name ;
// ...
unsignedlonglong id = next_id() ;
// path to file to store the last generated id
staticconstexprchar id_file_name[] = "last_generated_unique_id.txt" ;
staticunsignedlonglong next_id( unsignedlonglong default_id = 1001 ) {
unsignedlonglong id ;
{
// try to read from the last id from file;
// if successful, increment the id to get the next unique id
// otherwise, fall back to the default id
// (robust error reporting/handling elided for brevity)
std::ifstream file(id_file_name) ;
if( file >> id ) ++id ;
else id = default_id ;
}
// update the file with the freshly generated id
// (error handling elided for brevity)
std::ofstream(id_file_name) << id << '\n' ;
return id ;
}
};