Question: Please solve the error in this C++ Program, will rate the answer. //Include the header files #include #include #include #include #include #include #include #include #include
Please solve the error in this C++ Program, will rate the answer.
//Include the header files
#include #include
using namespace std;
//Main function
int main()
{
//Declare a linked list of node type string
std::list sList;
//Linked list iterator variable declaration
std::list::iterator iter;
//Local variables of type string
std::string parse,line, delim=" ",parse2, start= "", fname="testfile.txt";
//size_t type variable to find the size of the string
size_t ind=0;
//Integer type local variables declaration
int line_pos=1,s;
/*The while loop to check whether the editor is started correctly*/
while(start!="EDIT")
{
//Read the first line entered
getline(cin,line);
//Calculate the length of the string
int x=line.length();
//locate the space in the input line
ind=line.find(delim);
//Decrement the length of the string
x=x-ind;
//Parse the line for first word
start = line. substr(0,ind);
//If the first word entered is not "EDIT"
if(start!="EDIT")
{
/*Alert the user, indicates wrong start of editor*/
cout
}
//If the first word entered is "EDIT"
else
{
//Erase the first word from the input line
line.erase(0, ind+delim.length());
//Assign the index as end of the string
ind=x-1;
//Assign the second word to a string variable
parse2 = line. substr(0,ind);
/*Since the second word is the file name, append */
parse2.append(".txt");
//Assign the string as file name
fname= parse2;
}
//move iterator to beginning of the list
iter=sList.begin();
}
//The while loop to process the editor lines
while(1)
{
//print the line number and delimiter ">"
cout";
//Read the line
getline(cin,line);
//Local variables declarations
int line_num;
ind =0;
//locate the space in the input line
ind=line.find(delim);
//Parse the line for first word
parse = line. substr(0,ind);
//If the command is for insertion
if(parse=="I")
{
//The "I" is followed by a parameter
if(line.length()==3)
{
//Erase the first word, "I"
line.erase(0, ind+delim.length());
/*Assign the second word to string parse2*/
parse2 = line. substr(0,ind);
// String variable type of size_type
std::string::size_type sz;
//Convert the string to integer
line_num = std::stoi (parse2,&sz);
/*Move iterator to the beginning of the list*/
iter=sList.begin();
/*The for loop to move the iterator to the specified line*/
for(int i=1;i
{
//incremrnt iterator
++iter;
}
//Print the line number
cout";
//Read the line from user
getline(cin,line);
//Add the line to the list
sList.insert(iter,line);
//Increment the line position
line_pos++;
}
//If the command is only "I"
else if (line.length()==1)
{
//Print the line number
cout";
//Read the line
getline(cin,line);
/*Insert the line at current iterator position*/
sList.insert(iter,line);
//Increment the line position
line_pos++;
}
//If the line is not an insert command
else if (line.length()>3)
{
//Inset the line to the iterator position
sList.insert(iter,line);
//Increment the line position
line_pos++;
}
}
//If the command is to delete a line
else if(parse=="D")
{
//If D command has one parameter
if(line.length()==3)
{
//Erase the first word from the string
line.erase(0, ind+delim.length());
/*Store the parameter to a string variable*/
parse2 = line. substr(0,ind);
/*String size_type type variable declaration*/
std::string::size_type sz;
//Convert the string into integer type
line_num = std::stoi (parse2,&sz);
//move iterator to begining of the list
iter=sList.begin();
/*The for loop to traverse the iterator to the specific position*/
for(int i=0;i
{
//Increment the iterator
++iter;
}
//Erase the iterator position
iter=sList.erase(iter);
line_pos--;
}
//If command "D" has no parameters
else if (line.length()==1)
{
//Erase the current iterator position
iter=sList.erase(iter);
}
//I delete command "D" has two parameters
else if(line.length()==5)
{
/*Remove the pre-string of first parameter*/
line.erase(0, ind+delim.length());
//Parse the line for the first parameter
parse2 = line. substr(0,ind);
/*The size_type variable for integer conversion */
std::string::size_type sz;
//Convert string to integer
line_num = std::stoi (parse2,&sz);
/*Erase the string before the second parameter*/
line.erase(0, ind+delim.length());
//Parse the string for second parameter
string parse3 = line. substr(0,ind);
/*convert the parameter into integer type*/
static int line_num1 = std::stoi (parse3,&sz);
/*Integer variable to store the number of lines deleted*/
int line3=line_num1-line_num;
//move iterator to beginning of the list
iter=sList.begin();
/*The for loop to move to the location specified by first parameter*/
for(int i=1;i
{
//Increment the iterator
++iter;
}
/*The for loop to erase the lines using iterator position*/
for(int i=line_num;i
{
/*Erase the content iterator currently locates*/
iter=sList.erase(iter);
}
line_pos=line_pos-line3;
}
//If the line is not a proper delete command
else
{
/*Inset the line to the iterator position*/
sList.insert(iter,line);
//Increment the line position
line_pos++;
}
}
/*If the command is for listing the lines of the linked list*/
else if(parse=="L")
{
//Local variable declaration
int i=0;
//If the "L" command has only one parameter
if (line.length()==1)
{
/*The for loop to iterate the list from beginning to end*/
for(iter=sList.begin();iter!= sList.end();iter++)
{
/*Increment variable i, to represent the line number*/
i++;
}
//Move to the beginning of the list
iter=sList.begin();
/*The for loop to print the lines in the list*/
for(int idx = 1; idx
{
//Print the line with line number
std::cout "
//Increment the iterator
++iter;
}
}
//If the listing command has one parameter
else if(line.length()==3)
{
/*Erase the first word to get the parameter*/
line.erase(0, ind+delim.length());
//Assign the parametr to a string
parse2 = line. substr(0,ind);
//Variable of type size_type
std::string::size_type sz;
//Convert the string into integer
line_num = std::stoi (parse2,&sz);
//Local variable of type integer
int idx=1;
//move iterator to beginning of the list
iter=sList.begin();
/*The for loop to traverse the iterator to the specific position*/
for(int i=0;i
{
//Increment the iterator and index
++iter;
idx++;
}
//Display the iterator position
std::cout "
}
else if(line.length()==5)
{
/*Declare a local variable of type integer*/
int idx=1;
/*Remove the pre-string of first parameter*/
line.erase(0, ind+delim.length());
//Parse the line for the first parameter
parse2 = line. substr(0,ind);
/*The size_type variable for integer conversion */
std::string::size_type sz;
//Convert string to integer
line_num = std::stoi (parse2,&sz);
/*Erase the string before the second parameter*/
line.erase(0, ind+delim.length());
//Parse the string for second parameter
string parse3 = line. substr(0,ind);
/*Convert the parameter into integer type*/
int line_num1 = std::stoi (parse3,&sz);
//Move iterator to beginning of the list
iter=sList.begin();
/*The for loop to increment the line index and iterator*/
for(int i=0;i
{
/*Increment the iterator and index variables*/
++iter;
idx++;
}
/*The for loop to display the lines using iterator position*/
for(int i=line_num;i
{
//List the lines
std::cout "
/*Increment the iterator and index variables*/
++iter;
idx++;
}
}
//If the line is not a proper list command
else
{
/*Inset the line to the iterator position*/
sList.insert(iter,line);
//Increment the line position
line_pos++;
}
}
/*If the command was to append the lines in the list*/
else if(parse=="A")
{
if (line.length()==1)
{
int vi=0;
//Read the line
//Print the line number and >
cout";
//Read the line from user
getline(cin,line);
//Move the iterator to the beginning
iter=sList.begin();
/*The for loop to count the number of lines*/
for(iter=sList.begin();iter!= sList.end();iter++)
{
/*Increment variable i, to represent the line number*/
vi++;
}
//Move the iterator to the beginning
iter=sList.begin();
/*The for loop to save the contents to the file*/
for(int idx = 1; idx
{
/*Append the line to the all the lines*/
*iter+=line;
//Increment the iterator
++iter;
}
}
//If the line is not a proper append command
else
{
/*Inset the line to the iterator position*/
sList.insert(iter,line);
//Increment the line position
line_pos++;
}
}
//If command is E, save and exit
else if(parse=="E" &&line.length()==1)
{
//If the command was to save and exit
if (line.length()==1)
{
//Declare a file pointer
fstream file;
/*Local variable of type integer definition*/
int vi=0;
/*Open the file for appending the contents.*/
file.open(fname,fstream::in | fstream::out | fstream::app);
//Move the iterator to the beginning
iter=sList.begin();
/*The for loop to count the number of lines*/
for(iter=sList.begin();iter!= sList.end();iter++)
{
/*Increment variable i, to represent the line number*/
vi++;
}
//Move the iterator to the beginning
iter=sList.begin();
/*The for loop to save the contents to the file*/
for(int idx = 1; idx
{
//Write the lines to the file
file "
//Increment the iterator
++iter;
}
//Break the loop
break;
}
//If the line is not a proper exit command
else
{
/*Inset the line to the iterator position*/
sList.insert(iter,line);
//Increment the line position
line_pos++;
}
}
//If the line is not any proper command lines
else
{
//Inset the line to the iterator position
sList.insert(iter,line);
//Increment the line position
line_pos++;
}
}
//For visual studio command prompt freeze
system("pause");
//Exit the main function by return a "0" to it
return 0;
}
..................................................................................................
EXPECTED OUTPUT:

newfile - Notepad File Edit Format View Help 1> THE FIRST LINE APPENDED 2> INSERTED AT 3 APPENDED 3> THE VERY LAST LINE APPENDED
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
