I\'m having a minor problem when using putty c++ code, here are the instructions
ID: 3746817 • Letter: I
Question
I'm having a minor problem when using putty c++ code, here are the instructions and my output yet "Rich" doesn't appear in my final output, and it should be included, so I need help fixing this problem, code and text file is below. Thanks in advance.
// CODE
#include <iostream>
#include <cstdlib>
/* fstream header file for ifstream, ofstream,
fstream classes */
#include <fstream>
#include <vector>
using namespace std;
int main()
{
string line;
vector<string> vec ;
// Creation of ifstream class object to read the file
ifstream fin;
// by default open mode = ios::in mode
fin.open("C:\Users\user\Desktop\Transaction.txt");
// Execute a loop until EOF (End of File)
string cmd;//command
string inf;//information(string) to be put into vector
string ind;//index value read from input file
int index;//index value read from input file converted to int type
while (fin) {
// Read a Line from File
getline(fin, line);
int i=0;
cmd = "";
inf = "";
ind = "";
while(line[i]!='' && line[i]!=' ')//read command
{
cmd+=line[i];
i++;
}
if(cmd=="Add")//if command equals to Add then read the string to add and index
{
while(line[i]==' ')
i++;
while(line[i]!=' ')//read string
{
inf+=line[i];
i++;
}
while(line[i]==' ')
i++;
while(line[i]!='' && line[i]!=' ')//read index
{
ind+=line[i];
i++;
}
index = atoi(ind.c_str());//convert index from string type to int type
vec.insert(vec.begin()+index, inf);//add content at the index read
}
else if(cmd=="Remove")//if command equals to remove then read the index from which the string has to be removed
{
while(line[i]==' ')
i++;
while(line[i]!='' && line[i]!=' ')//read index
{
ind+=line[i];
i++;
}
index = atoi(ind.c_str());//convert index from string type to int type
vec.erase(vec.begin()+index);//remove content from the index read
}
else if(cmd=="Print")//if command equals to print then print all contents of vector
{
for (vector<string>::const_iterator it = vec.begin(); it != vec.end(); ++it)
cout << *it << ' ';
cout<<endl;
}
}
// Close the file
fin.close();
}
// Transaction.txt
Add Student 0
Add Kid 0
Add Final 1
Add Grow 1
Add Note 2
Print
Add Rich 5
Remove 1
Remove 7
Add Mind 2
Remove 3
Print
Explanation / Answer
Hello,
You code has some minor problem where you use vec.erase(vec.begin()+index);
erase function in vector reduce the size of vector by number of element you want to erase (In your case it reduced the size of vector by 1, because you erase by index) after that it copy all elements except the one whose index is given.
When program get command Remove 7 it reduce the vector size by 1 since 7 is not an index in vector so last element of your vector are removed from the vector due to size is reduced by 1.
Just check condition of index as
if(index<vec.size())
{
vec.erase(vec.begin()+index);//remove content from the index read
}
and your code work fine...
here is updated code...
//Updated Code
#include <iostream>
#include <cstdlib>
/* fstream header file for ifstream, ofstream,fstream classes */
#include <fstream>
#include <vector>
using namespace std;
int main()
{
string line;
vector<string> vec ;
// Creation of ifstream class object to read the file
ifstream fin;// by default open mode = ios::in mode
fin.open("Transaction.txt");
// Execute a loop until EOF (End of File)
string cmd;//command
string inf;//information(string) to be put into vector
string ind;//index value read from input file
int index;//index value read from input file converted to int type
while (fin)
{
// Read a Line from File
getline(fin, line);
int i=0;
cmd = "";
inf = "";
ind = "";
while(line[i]!='' && line[i]!=' ')//read command
{
cmd+=line[i];
i++;
}
if(cmd=="Add")//if command equals to Add then read the string to add and index
{
while(line[i]==' ')
i++;
while(line[i]!=' ')//read string
{
inf+=line[i];
i++;
}
while(line[i]==' ')
i++;
while(line[i]!='' && line[i]!=' ')//read index
{
ind+=line[i];
i++;
}
index = atoi(ind.c_str());//convert index from string type to int type
vec.insert(vec.begin()+index, inf);//add content at the index read
}
else if(cmd=="Remove")//if command equals to remove then read the index from which the string has to be removed
{
while(line[i]==' ')
i++;
while(line[i]!='' && line[i]!=' ')//read index
{
ind+=line[i];
i++;
}
index = atoi(ind.c_str());//convert index from string type to int type
if(index<vec.size())
{
vec.erase(vec.begin()+index);//remove content from the index read
}
}
else if(cmd=="Print")//if command equals to print then print all contents of vector
{
for (vector<string>::const_iterator it = vec.begin(); it != vec.end(); ++it)
cout << *it << ' ';
cout<<endl;
}
}
// Close the file
fin.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.