How to check if input is valid, if not reask question.

I understand (!cin) but I need to check it for multiple user input variables, how do I do this?

And I also don't understand cin clear and ignore, so if the user doesn't meet the requirements how do I cout an error message and then reroute the user to reanswer the question.

Try this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
double cinCheck ()
{
	double amount;
	bool loopEnd;
	
	do
	{
		cin >> amount;
		
		if (cin.fail()) // make sure user does not enter a letter
		{
			cout << "Error. Entry must be a number. \n: ";
			cin.clear();
			cin.ignore(INT_MAX, '\n');
			
			loopEnd = false;
		}
		else
		{
			loopEnd = true;
		}
	}while(!loopEnd);
	
	return amount;
}


Best,
max
It depends on what you mean by "valid", but to check for valid integers you could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <limits>
#include <cstdlib>

bool reset(std::istream& in)
{
    bool eof = in.eof();
    in.clear();
    if (!eof) in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return eof;
}

int main()
{
    using std::cout, std::cin;
    int val {};
    while (true)
    {
        cout << "Enter an integer: ";
        if (cin >> val) break;
        if (reset(cin))
        {
            cout << "\neof entered\n";
            std::exit(0);
        }
        cout << "Bad value!\n";
    }
    cout << "You entered: " << val << '\n';
}

Or perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <limits>

bool reset(std::istream& in)
{
    bool eof = in.eof();
    in.clear();
    if (!eof) in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return eof;
}

int get_int(const std::string& prompt)
{
    int val {};
    while (true)
    {
        std::cout << prompt << ": ";
        if (std::cin >> val) break;
        reset(std::cin);
        std::cout << "Bad value!\n";
    }
    return val;
}

int main()
{
    using std::cout, std::cin;
    int val = get_int("Enter you age");
    cout << "You entered: " << val << '\n';
}

Last edited on
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <limits>

// Get an integer with prompt
int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}


int main() {
	const auto age {getInt("Enter your age: ")};

	std::cout << "You entered: " << age << '\n';
}

> I understand (!cin) but I need to check it for multiple user input variables
chain cin >> a >> b >> c
if one of the input fails, the ones to the right also fail.

> And I also don't understand cin clear and ignore
if the input fails, cin remains in a failed state and the next input operation will also fail (see above)
so you need to clear the failed state, and for that you may use clear()

given that the input failed, nothing was read, so whatever the user entered remains to be read. perhaps you want to discard that invalid input that make it fail, ignore a couple of characters and continue with the rest, and for that you may use ignore()
Topic archived. No new replies allowed.