I'm currently working on a program that has a linked list. My program need to have a function that can sort all the data based on its months and then display all of the data but I can't find a good example for me to refer because in my case I need to search the string expireDate;
which is input this way dd/mm/yyyy which means that I need to take its subtring by using std::string str2 = temp->expireDate.substr(3,2);
in order for me to get the month and then convert the substring into integer using int month;
. Please take a look at my code, it is sorted but it doesn't display anything when I run it, I think there must be a very little mistake. I've already figured out my mistakes, it's just a silly mistake actually. There's no error on this code tho.
carInsurance* remove_next(carInsurance* prev)// pass NULL if start_ptr is the Node to be removed
{
if(prev)
{
if( prev->next )// ensure prev isn't pointing to the last Node in the list
{
carInsurance* temp = prev->next;// temp will be removed
prev->next = temp->next;// link across temp
return temp;
}
}
else if(start_ptr)// ensure list not empty
{
carInsurance* temp = start_ptr;// start_ptr will be removed
start_ptr = start_ptr->next;
return temp;
}
return NULL;// if called on empty list, or if prev->next is NULL
}
void carInsurance::sortMonth(int *x) // sort by month in ascending order
{
carInsurance *start_ptr2 = NULL;// head of sorted list
float price, addPrice;
while(start_ptr)// repeat until no nodes left in unsorted list
{
// pointers for iterating through the unsorted list
carInsurance *prev = NULL;// always previous to the current node considered (below)
carInsurance *curr = start_ptr;// start at beginning of list
carInsurance *prevMax = NULL;// pointer to node before the node with the highest age
int max = start_ptr->month;// 1st node holds max age to start
while(curr)// iterate through the list
{
if(curr->month > max )// new highest age found
{
max = curr->month;// save new max age
prevMax = prev;// save pointer to node before the max
}
prev = curr;// advance iterators
curr = curr->next;
}
// Node with the highest age found this pass through the list.
carInsurance *xferNode = remove_next(prevMax);// obtain node to be moved into sorted list
if( xferNode )// check that it's not NULL
{
xferNode->next = start_ptr2;// add to beginning of sorted list
start_ptr2 = xferNode;
}
}
start_ptr = start_ptr2;// list now sorted. Reassign start_ptr to point to it.
while(temp != NULL)// Display details for what temp points to
{
cout << "\n___Customer Information___\n" << endl;
cout << "\nName : " << temp->name << endl;
cout << "IC: "<< temp->iCno << endl;
cout << "Date of Birth: " << temp->dob << endl;
cout << "Nationality: " << temp->nationality << endl;
cout << "Address: " << temp->address << endl;
cout << "Mobile Number: " << temp->phoneNo << endl;
cout << "Email: " << temp->email << endl;
cout << "Occupation: " << temp->occupation << endl;
cout << "\n_____Car Information_____\n" << endl;
cout << "Car Plate Number: " << temp->carNo << endl;
cout << "Insurance Expire Date: " << temp->expireDate << endl;
cout << "Usage of Car Model/Make: " << temp->carUsage << endl;
cout << "Manufacturing Date: " << temp->manufacturingDate << endl;
cout << "\n___Insurance Information___\n" << endl;
cout << "Insurance Package:" << endl;
if(temp->package == 1)
{
cout << "\nPackage A (Comprehensive Cover)" << endl;
cout << "\t-Covers losses or damages to your car due to accident, fire and theft " << endl;
cout << "\t-Covers Third Party death and bodily injuries " << endl;
cout << "\t-Covers Third Party property losses or damages " << endl;
price = 1000;
}
else if (temp->package == 2)
{
cout << "\nPackage B (Third Party Cover)" << endl;
cout << "\t-Covers Third Party death and bodily injuries" << endl;
cout << "\t-Covers Third Party property losses or damages" << endl;
price = 1500;
}
else if (temp->package == 3)
{
cout << "\nPackage C (Third Party, Fire & Theft Cover)" << endl;
cout << "\t-Covers losses or damages to your car due to fire or theft" << endl;
cout << "\t-Covers Third Party death and bodily injuries" << endl;
cout << "\t-Covers Third Party property losses or damages" << endl;
price = 900;
}
else
cout << "No package available" << endl;
if(temp->additional == 1)
{
cout << "\nAdditional package: "<< endl;
if (temp->option==1){
cout << "Road tax renewal" << endl;
addPrice = 50;
}
else if (temp->option==2){
cout << "Name of second driver" << endl;
addPrice = 0;
}
else if (temp->option==3){
cout << "Windscreen coverage" << endl;
addPrice = 20;
}
else if (temp->option==4){
cout << "Rental reimbursement" << endl;
addPrice = 50;
}
else if (temp->option==5){
cout << "Roadside assistance or towing insurance" << endl;
addPrice = 80;
}
else if (temp->option==6){
cout << "Full glass coverage" << endl;
addPrice = 70;
}
else
cout << "Not availabe"<< endl;
}
else{
cout << "No additional package" << endl;
price = 0;
}
temp->insuranceAmount = price + addPrice;
cout << "Amount to be insured: RM" << temp->insuranceAmount << endl;
//temp = temp->next;
}//End of While Loop
}//End of sortMonth