Checking if input is an integer

I have this function int GetIntInput(int min, int max)


And I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int  GetIntInput(int min, int max)
{
/* Your code goes here
Note: you should call your ValueInRange function to validate the user's input!
This function must also check that the user enters integer data,
and issue an error message and re-prompt the user if they make a mistake. */
int i;
cin >> i;

while (!(ValueInRange(i,min,max)))
{
    cout << "Error. Please enter a valid input: ";
    cin >> i;
}

return i;


So in order to explain it, this is a function in a bigger program. I have to fill out the insides of it with the instructions in the comment.

Right now, I'm able to check using the 'ValuesInRange' function, whether that i that was inputted from the user is between the min and max.

The problem is, I don't know how to check if that i is an integer or not. If the user enters like 4.5, thats an integer! And shouldn't work! But what ends up happening, that 4.5 becomes a 4, and it says like "yes this value 4 is between the min and max".

But it shouldn't do that, it should have a way in the function of checking whether or not its an integer, like 4,5,6 not 4.5 5.4 6.9.

Does anyone know how to do that? Something that I can understand too, not hard so that I can put it in my function.

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
    double i;
    cin >> i;
    



	while (!(ValueInRange(i,min,max)) && (i != static_cast<int>(i)) )
    {
        cout << "Error. Please enter a valid input: ";
        cin >> i;
        cin.ignore(80, '\n');
    }


I tried this, but it doesn't work. Still treats 4.5 as a 4, and therefore proceeding with the program when I want it to stop and ask again until its actually a 4 or a different integer
1
2
3
4
5
6
7
8
9
10
  double i;
    cin >> i;

	while (!(ValueInRange(i,min,max)) && (i/1.00 != static_cast<int>(i)) )
    {
        cout << "Error. Please enter a valid input: ";
        cin >> i;
    }

	return i;


AFter some more googling, that's what I thought of using.

I would think that would work, but it doesn't. (I got it through some more googling).
The logic there is, if we divide i by 1.00, it becomes a double right? So if its not equal to the casted into an int (the variable i), then ask for another input. That logic makes sense though..this question is hard..

Can someone help, this is making me sad that I can't get it..
Here is a fully working sample..

WARNING: however keep in mind if you need this for to complete your home work you teacher will know that somebody else helped you and won't accept this solution:

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
31
32
33
34
35
36
#include <string>
#include <cctype>
#include <iostream>
#include <cstdlib>

bool ValueInRange(const std::string& input, int& min, int& max);

int main()
{
	using namespace std;
	string input;
	int max = 10;
	int min = 0;

	for (;;)
	{
		cin >> input;

		if (ValueInRange(input, min, max))
			cout << "Input value is OK" << endl;
		else
			cout << "Input value is bogus" << endl;
	}
	return 0;
}


bool ValueInRange(const std::string& input, int& min, int& max)
{
	if (!input.empty() && std::find_if(input.begin(), input.end(), [](char c) { return !std::isdigit(c); }) == input.end())
	{
		int value = atoi(input.c_str());
		return (value < max && value > min);
	}
	return false;
}
Thanks for that. I know, I'm not going to copy and paste anything. Even if I did, I can't exactly use that, I don't know what half the things are..it's only an intro to c+ course.

But what about that logic in the code above? I tried to do this:
1
2
3
4
5
6
7
8
9
10
int i;
    cin >> i;

	while (!(ValueInRange(i,min,max)) && (i/1.00 != static_cast<int>(i)) )
    {
        cout << "Error. Please enter a valid input: ";
        cin >> i;
    }

	return i;


The logic where it checks if i/1.00 (a value thats a double), is its self when i is casted into int. This makes sense..but doesn't work. This should be easier than this..shouldn't have to take 3 hours 2:(
Last edited on
You can achieve same logic withing one 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
26
27
28
bool ValueInRange(float input, int min, int max);
int main()
{
	using namespace std;
	float input;
	const int max = 10;
	const int min = 0;

	for (;;)
	{
		cin >> input;

		if (ValueInRange(input, min, max))
			cout << "Input value is OK" << endl;
		else
			cout << "Input value is bogus" << endl;
	}
	return 0;
}


bool ValueInRange(float input, int min, int max)
{
	if (input / static_cast<int>(input) == 1)
		if (input > min && input < max)
			return true;
	return false;
}
Last edited on
The problem with i != static_cast<int>(i) is that the program will cast i to an int on the right side, then realize the i on the left side is a double/float, so it will then cast i on the left side to an int. When i is cast to an int on both sides, it will always be the same. So, what you should do is force the program to get a double/float on the right side, so when it tries to compare, it won't cast both of them to an int.

I don't know what you're doing about character and string inputs, but you can invalidate that by a conditional on cin>>i. I'll let you look that up yourself.
I found this on stack

1
2
3
4
5
6
int x;
cin >> x;

if (cin.fail()) {
    //Not an int.
}

http://stackoverflow.com/questions/10405312/c-ostream-friends-and-namespaces
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
	int input;
	cout << "Enter an int:  ";
	cin >> input;
	if (cin.fail())
		cout << "Not an int\n";
	return 0;
}


Compile and run that, then enter a floating point number (I tried 1.5).
Last edited on
Topic archived. No new replies allowed.