please clear the error in this program getting error in function void ReadFile()
ID: 3738567 • Letter: P
Question
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<iostream>
#include<fstream>
#include<cstring>
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.."<<endl;
exit(0);
}
}
// Adds record to tail of linked list
void AddRecord(long PhoneNo, char *Name, char *Address){
cout<<PhoneNo<<" : "<<Name<<" : "<<Address<<endl;
PhoneRecord *Tmp = new PhoneRecord;
Tmp->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"<<endl;
}
/*
*
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"<<endl;
}else{
cout<<"Head record removed:"<<endl;;
DisplayRec(Head);
PhoneRecord *Tmp = Head;
Head = Head->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<<RecPtr->Name<<endl;
cout<<RecPtr->Address<<endl;
cout<<RecPtr->PhoneNo<<endl;
cout<<endl;
}
// Deletes memory in list
void DeleteList(){
PhoneRecord *Crnt = Head;
while(Head != NULL){
PhoneRecord *Tmp = Head;
Head = Head->Next;
delete Tmp;
}
cout<<"List deleted"<<endl;
/*
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"
*/
}
Explanation / Answer
The code works fine for me, no errors are thrown. So try below options:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.