cannot delete from empty list
Apr 13, 2017 at 1:11pm UTC
cannot delete from empty list
user inputs 1 line
user deletes 1 line
user deletes 3rd line which is nothing *crashes here*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#include <iostream>
#include <string>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
const int NUM_LINES = 5;
template < typename ITERATOR >
ITERATOR my_next( ITERATOR iter, typename iterator_traits<ITERATOR>::difference_type n = 0 )
{
advance( iter, n ) ;
return iter ;
}
void inputf(list<string> &L);
void deleteone(list<string> &L);
void deleteall(list<string> &L);
void print(list<string> &L);
int main()
{
cout << "This program will take in lines up to 5 you have inputed and you can delete, add, or print to the list." << endl;
list <string> myList;
list <string>::iterator it = myList.begin();
int repeat;
do {
//input line block
if (repeat == 1)
{
inputf(myList);
if (myList.size() > 5)
{
cout << "List is full. Cannot add more to list, please delete a line." << endl;
repeat = 2;
}
}
//delete one line
if (repeat == 2)
{
deleteone(myList);
}
//print block
if (repeat == 4)
{
print(myList);
}
cout << endl;
cout << "What do you want to do:" << endl
<< "1. Add a line" << endl
<< "2. Delete a line" << endl
<< "4. Print all the lines" << endl
<< "Please enter a number above: " ;
cin >> repeat;
}while (repeat >= 1 && repeat <= 5);
return 0;
}
//input function
void inputf(list<string> &L)
{
char input[40];
int lines;
cout << "How many lines do you want to enter: " ;
cin >> lines;
if (lines <= 0)
{
cout << "WHAT?! You have to input something. Enter again: " ;
cin >> lines;
}
cout << "Enter lines" ;
cout << endl;
for (int ins = 0; ins <= lines; ins++)
{
cin.getline(input,40);
L.push_back(input);
}
}
//print function
void print(list<string> &L)
{
cout << endl;
if (L.empty())
{
cout << "This list is empty." << endl;
}
list<string>::iterator inx = L.begin(); //inx = 5; // Can only assign iterator values
for (inx = L.begin(); inx != L.end(); ++inx)
cout << *inx << '\n' ;
}
//delete function
void deleteone(list<string> &L)
{
list<string>::iterator inx = L.begin();
if (L.size() < 1)
{
cout << "List is empty. Cannot delete lines from list." << endl;
L.clear();
}
else
{
int choose;
cout << "Which line to delete? (Line 1 is 1, Line 5 is 5): " ;
cin >> choose;
if (choose < 1 || choose > 5)
{
cout << "Invalid input, enter a number between 1 and 5: " ;
cin >> choose;
}
advance(inx,choose);
L.erase(inx);
}
}
Last edited on Apr 13, 2017 at 1:22pm UTC
Apr 13, 2017 at 3:27pm UTC
cannot delete from empty list
This is correct. using advance(...) The index must always be less than L.size().
Note that you are addressing the
second element of the list when
choose = 1
.
Thus, before you access a list element via index, you need to check whether the index is within the bounds of the list not an arbitrary bound like 5. It is a list not an array.
Topic archived. No new replies allowed.