This is the code I have made, however, what is being displayed is incorrect. Kindly teach me what do I need to fix.
#include <iostream>
#include <list>
using namespace std;
bool check(int passing){
int g;
if(g<=passing){
return false;
}else{
return true;
}
}
int main()
{
int pg;
cout<<"What is the passing grade?"<<endl;
cin>>pg;
list<int> grades = {100,90,93,95,92,98,97,99,96,94};
grades.remove_if(check);
for(int x : grades){
cout<<x<<'\t';
}
return 0;
}
Use a lambda as a predicate for remove_if
like below:
#include <iostream>
#include <list>
int main( )
{
std::cout << "What is the passing grade?\n";
int passingGrade { };
std::cin >> passingGrade;
std::list<int> grades { 100, 90, 93, 95, 92, 49, 50, 98, 97, 99, 11, 94 };
/*grades.remove_if( [ &passingGrade ]( const int grade ) // lambda approach
{
return ( grade < passingGrade ) ? true : false;
}
);*/
for ( auto it = grades.begin( ); it != grades.end( ); ) // for-loop approach
{
if ( *it < passingGrade )
{
it = grades.erase( it );
}
else
{
++it;
}
}
for ( const int grade : grades )
{
std::cout << grade << '\t';
}
}
Sample I/O
What is the passing grade?
50
100 90 93 95 92 50 98 97 99 94
Another sample:
What is the passing grade?
95
100 95 98 97 99