Question: please clear the error in this program getting error in function void ReadFile() that 'stol' was not declared in this scope /* Note : The

please clear the error in this program

getting error in function void ReadFile() that 'stol' was not declared in this scope

/*

Note : The comments that were qlready present are left as they were, only the implementation part was changed, kindly note this.

*/

// put your name etc here...

#include

#include

#include

using namespace std;

// ============= User Defined types ==================================

// linked list record

struct PhoneRecord{

long PhoneNo;

char Name[20];

char Address[40];

PhoneRecord *Next; // pointer to next record in list

};

// ============= Global Data =========================================

const char cDataFileName[] = "phone.txt";/*This file must be present in the directory where the binary was being run*/

PhoneRecord *Head; // head of linked list

// ============= Function Prototypes =========================

void Initialise();

void ReadFile();

void AddRecord(long PhoneNo,char *Name,char *Address);

void FindRecord();

void RemoveRecord();

void DisplayAllRecs();

void DisplayRec(PhoneRecord *Rec);

void DeleteList();

// ============= Main Function ================================

int main(){

cout << "Begin test" << endl << endl;

Initialise();

ReadFile();

FindRecord();

RemoveRecord();

FindRecord();

DisplayAllRecs();

DeleteList();

cout << endl << "End test" << endl;

return 0;

}

// ============= Function Definitions =========================

//Initialises linked list

void Initialise(){

Head = NULL;

}

// Reads data file into linked list

void ReadFile(){

/*

declare ifstream and open file in cDataFileName

if( file does not open display file not found msg and exit

while not eof

read PhoneNo

if read fails break while loop

ignore (eat) end of line left over from fin>>PhoneNo

read whole name with getline()

read whole address with getline()

AddRecord(PhoneNo,Name,Address); // to the list

end while

close the file

display the number of records read from the file

*/ std::ifstream ifs(cDataFileName); if(ifs.is_open()){ string line; int counter = 0; long phNumber; char Name[20] = {0}; char Address[40] = {0};

while(getline(ifs,line)){ if(counter == 0){ phNumber = stol(line); }else if(counter == 1){ memcpy(Name,line.c_str(),line.length()); }else if(counter == 2){ memcpy(Address,line.c_str(),line.length()); AddRecord(phNumber,Name,Address); } counter = (counter + 1) % 3; } }else{ cout<<"file not found.."<

}

// Adds record to tail of linked list

void AddRecord(long PhoneNo, char *Name, char *Address){ cout<PhoneNo = PhoneNo; memcpy(Tmp->Name,Name,strlen(Name)); memcpy(Tmp->Address,Address,strlen(Address)); Tmp->Next = NULL; if(Head == NULL){ Head = Tmp; }else{ PhoneRecord *Crnt = Head; while(Crnt->Next != NULL){ Crnt = Crnt->Next; } Crnt->Next = Tmp; } /*

declare a Tmp PhoneRecord pointer

allocate new PhoneRecord to Tmp ptr

set Tmp->PhoneNo to PhoneNo;

string copy Name into Tmp->Name

string copy Address into Tmp->Address

set Tmp->Next to NULL

if Head is NULL set Head = Tmp;

else{

declare PhoneRecord *Crnt and set to Head;

while Crnt->Next is not NULL

set Crnt to Crnt->Next

end while

set Crnt->Next to Tmp;

end else

*/

}

// Finds record in linked list and displays it

void FindRecord(){ long PhoneNo; cout<<"Enter phone number to find: "; cin>>PhoneNo; cout<<" "; PhoneRecord *Crnt = Head;

while(Crnt != NULL && Crnt->PhoneNo != PhoneNo){ Crnt = Crnt->Next; } if(Crnt != NULL){ DisplayRec(Crnt); }else{ cout<<"Record not found"<

declare long PhoneNo

display "Enter phone number to find: "

get PhoneNo from user

declare PhoneRecord *Crnt and set to Head

while Crnt is not NULL AND Crnt->PhoneNo is not equal to PhoneNo

Crnt = Crnt->Next;

end while

if Crnt is not NULL

DisplayRec(Crnt)

else

display "Record not found"

*/

}

// Removes record from head of linked list

void RemoveRecord(){ if(Head == NULL){ cout<<"List is empty"<Next; delete Tmp; } /*

if Head is NULL display "List is empty"

else

display "Head record removed:

DisplayRec(Head)

declare PhoneRecord *Tmp and set to Head;

set Head to Head->Next

delete Tmp;

end else

*/

}

// Prints all records on screen one at a time

void DisplayAllRecs(){

char Ans;

PhoneRecord *Crnt;

Crnt = Head;

while(Crnt!=NULL){

DisplayRec(Crnt);

cout<<"Display next record: ";

cin>>Ans;

if(Ans=='n') break;

Crnt=Crnt->Next;

}

cout << endl;

}

// Displays record on screen

void DisplayRec(PhoneRecord *RecPtr){

cout<Name<

cout<Address<

cout<PhoneNo<

cout<

}

// Deletes memory in list

void DeleteList(){ PhoneRecord *Crnt = Head; while(Head != NULL){ PhoneRecord *Tmp = Head; Head = Head->Next; delete Tmp; } cout<<"List deleted"<

declare PhoneRecord *Crnt and set to Head

while Head is not NULL

declare PhoneRecord *Tmp and set to Head

set Head to Head->Next

delete Tmp

end while

display "List deleted"

*/

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!