Get only intended input from user through the console I finally worked out all of the bugs in this bit of code. I'm pretty proud of it and I wanted to give it to all of you so that you could have it if you wanted it for any console programs that you make.

At the same time, this is ridiculous. C++ is double my age and there should seriously be a better way of getting valid, error free, inputs from the user. If anyone knows, please tell me.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

void gValid_Input(std::string&amp; var, std::string question) {
    using namespace std;

    do {
        cin.clear();
        cin.sync();
        cout &lt;&lt; question;

        // Code will not work as intended on Linux without the following
        // but will work on windows (tested with g++)
        // this must also be done after clearing, syncing cin
        // and asking the question string otherwise the code will loop forever
        if (cin.peek() == '\n'){
            cin.ignore(1,'\n');
        }
    } while (!(getline(cin, var)));
}

template &lt;typename t&gt;
void gValid_Input(t&amp; var, std::string question) {
    using namespace std;

    do {
        cin.clear();
        cin.sync();
        cout &lt;&lt; question;
    } while (!(cin &gt;&gt; var));
}

void gValid_Option(char&amp; response, std::vector&lt;char&gt; valid_Responses = {'y','n'}){
    using namespace std;
    const char diff = 'a' - 'A'; 

    do{
        cin.clear();
        cin.sync();
        cin &gt;&gt; response;

        // Comment the following out if you care about getting an upper case answer
        if (response &gt;= 'A' &amp;&amp; response &lt;= 'Z'){
            response += diff;
        }

    } while (find(valid_Responses.begin(), valid_Responses.end(), response) == valid_Responses.end());
}

void gValid_Option(char&amp; response, std::string question, std::vector&lt;char&gt; valid_Responses = {'y','n'}){
    using namespace std;
    const char diff = 'a' - 'A';

    do{
        cin.clear();
        cin.sync();
        cout &lt;&lt; question;
        cin &gt;&gt; response;

        // Comment the following out if you care about getting an upper case answer
        if (response &gt;= 'A' &amp;&amp; response &lt;= 'Z'){
            response += diff;
        }

    } while (find(valid_Responses.begin(), valid_Responses.end(), response) == valid_Responses.end());
}
I see some things that may help you improve your program.

Use const references where practical

The code currently declares its first function like so:

void gValid_Input(std::string&amp; var, std::string question);


However the passed question is not and should not be altered by this code, so better would be to pass a const std::string&amp; question or std::string_view question if you have a C++17 compiler.

Use return values to return values

Each of the functions is defined as returning void but each is actually returning a single value which is the response.  So for the first function, including the advise listed above, one could instead make the signature of the first function this:

std::string gValid_Input(const std::string&amp; question);


Fix the bug

When the user inputs the end of file marker (Ctrl+D in Linux), the prompt just repeats endlessly in a loop.

Consolidate the code

All four of the functions could be represented with a single templated function:

template &lt;typename T&gt;
T gValid_Input(const std::string&amp; prompt, bool (*isValid)(const T&amp;) = [](const T&amp;){ return true; }) {
    T var;
    while (std::cout &lt;&lt; prompt &amp;&amp; !((std::cin &gt;&gt; var) &amp;&amp; isValid(var))) {
        if (std::cin) {
            std::string line;
            std::getline(std::cin, line);
            std::cout &lt;&lt; var &lt;&lt; line &lt;&lt; " is not a valid answer\n";
        } else {
            std::cin.clear();
            std::string line;
            std::getline(std::cin, line);
            std::cout &lt;&lt; line &lt;&lt; " is not a valid answer\n";
        }
    }
    std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
    return var;
}


Here's are some example of its use:

#include &lt;complex&gt;
#include &lt;array&gt;

int main() {
    auto answer{gValid_Input&lt;std::string&gt;("What is your name? ")};
    std::cout &lt;&lt; "Your answer: \"" &lt;&lt; answer &lt;&lt; "\"\n";
    auto age{gValid_Input&lt;int&gt;("What is your age? ")};
    std::cout &lt;&lt; "Your answer: \"" &lt;&lt; age &lt;&lt; "\"\n";
    auto cpx{gValid_Input&lt;std::complex&lt;double&gt;&gt;("What is your favorite complex number? ")};
    std::cout &lt;&lt; "Your answer: \"" &lt;&lt; cpx &lt;&lt; "\"\n";
    auto weekday{gValid_Input&lt;std::string&gt;("What is your favorite day of the week? ", 
        [](const std::string &amp;day){ const std::array&lt;std::string, 7&gt; weekdays{
            "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" 
            };
            return std::find(weekdays.begin(), weekdays.end(), day) != std::end(weekdays);
        }
    )};
    std::cout &lt;&lt; "Your answer: \"" &lt;&lt; weekday &lt;&lt; "\"\n";
    auto fun{gValid_Input&lt;char&gt;("Are you having fun? ", 
        [](const char &amp;ch){ return ch=='y' || ch=='n'; }
    )};
    std::cout &lt;&lt; "Your answer: \"" &lt;&lt; fun &lt;&lt; "\"\n";
}


I'm using a lambda function as the validator, but it could also be any function with the appropriate signature.

Reconsider the interface

It would be nice to be able to use streams other than std::cin and std::cout.  I'd recommend passing the input and output streams as parameters.