I was asked to: define integer list; ask the user to enter at least 5 numbers; t
ID: 3625689 • Letter: I
Question
I was asked to: define integer list; ask the user to enter at least 5 numbers; to erase the middle number using c.erase(p). then display the element.Here is my attempt
#include <iostream>
#include <list>
using namespace std;
int main ()
{
cout<<"Enter at least five integer Numbers" <<endl;
mylist<int> input_numbers;
unsigned int i;
list<unsigned int> mylist;
for (i=1; i<=5; i++) mylist.push_back(i);
mylist.erase (mylist.size()/2);
cout << "mylist contains:";
for (i=0; i<mylist.size(); i++)
cout << " " << mylist[i];
cout << endl;
return 0;
}
It did not work. Should I use while loop?
thank you
Explanation / Answer
Try this
--------------------------------------------------------
#include <iostream>
#include <list>
using namespace std;
int main ()
{
int l = 0;
unsigned int k = 0;
list<unsigned int> mylist;
list<unsigned int>::iterator it1,it2, it3;
bool Continue = true;
cout<<"Enter at least five integer Numbers enter (0) to stop: " << endl;
for (int j = 0; (Continue); j++) // I changed you read in method because i couldn't figure out how your were doing it
{
cout << "Integer " << (mylist.size () +1) << ": ";
cin >> l;
k = l;
if (l == 0)
if (j < 5)
Continue = true;
else
Continue = false;
else
mylist.push_back (k);
}
it1 = mylist.begin ();
for (int j = 0; j < (mylist.size ()/2); j++)
{
it1++; // I used the for loop to count the iterator up to the right spot
}
mylist.erase (it1);
cout << "mylist contains:";
int size = mylist.size ();
for (int j = 0; j < size; j++)
{
cout << " " << mylist.front ();
mylist.pop_front ();
}
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.