#include <iostream>
#include <stdlib.h>
#include <limits>

bool invalid_input(std::istream& in) {
    return in.rdstate() == std::ios::failbit;
}

std::istream& get_single_grade(std::istream& in, int& grade) {
    std::cout << "Please enter a grade value between 0 and 100." << "\n";
    if (in >> grade) {
        if(grade<0 || grade>100)
            in.setstate(std::ios::failbit);
    }
    return in;
}

bool get_grade(std::istream& in, int &grade) {
    while(invalid_input(get_single_grade(in, grade))) { //while we failed to get data
         in.clear(); //clear the failure flag
         //ignore the line that the user entered, try to read the next line instead
         in.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 
    }
    return in.good();
}

int main(int argc, char* argv[])
{    
    int grade = -1; // grade will hold grade value; initialized to -1
    if (get_grade(std::cin, grade) == false) {
        std::cerr << "unxpected EOF or stream error!\n";
        return false;
    }    
    std::cout << grade << "\n";
    return EXIT_SUCCESS;
}