#include <iostream>
#include <list>
usingnamespace std;
int main()
{
list<int> lst;
int val;
while(cin >> val)
{
while(val % 2 != 0)
{
cout << val << " is not even number \nTry again...\n";
cin >> val;
}
lst.push_back(val);
}
cout << "\nList values : \n";
list<int>::iterator p = lst.begin();
while(p != lst.end())
{
cout << *p << " ";
p++;
}
return 0;
}
This program accepts only even numbers.
It is work well with me but when do I print values to the screen from 1 to 9 then I get out from program by pressing any button is not digital, the program prints the number zero at the end of the list, although that I did not print this value
Add lst.pop_back(); after your while loop. I believe what's happening is that you're adding in the "garbage" in the buffer. The pop_back will remove the last item inserted (the garbage).
Another alternative is to use something besides a (cin >> val) condition.
while(cin >> val)
{
while(checkingValue(val))
{
cout << val << " is not even number \nTry again...\n";
cin >> val;
}
lst.push_back(val);
}
lst.pop_back();
But now appears new problem when I add 1 then 2, it didn't print 2 on the screen
This is because . represents 0.0. This is the int 0. 0 is even (according to your while statement) so it get's added to your list. You display numbers 2 4 6 8 0 because all of those numbers are in your list.