Rejecting a floating point

Hello, I am very new to C++ and in the process of trying to write a programme. Part of the programme will be to work out a factorial amount which I have working, however I want the user to have to enter a whole number (not 0, negative or floating point) For simplicity I have written a basic example but I can not figure out how to write a "If entry = float, then error message. Am i missing something? unfortunately I have been trying to learn via google and youtube. Any help would be appreciated, even if its a "You need to google X,Y,Z" Thanks in advance

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
 
#include <iostream>
#include <string>

using namespace std;

int main()
{
 int n;
 
 cout <<"Please enter a number:  " << endl;
 cin >> n ; 
 
 if(n == 0)
 
 {
 cout << "You entered 0, please enter a whole number greater than 0" << endl; 
 }
 
 if(n < 0) {
     
     cout <<"You have entered a negative number" << endl;
     
 }
 
 else {
     
     cout <<"you entered: " << n << endl;
     
 }
 return 0;
 
}
If you >> into an int then you can't detect specifically if the user entered a fractional value. You can only detect if they entered a non-number.
1
2
if (!(std::cin >> n))
    //something other than an integer was entered 
The comparison will be true if the user enters anything that can't be an integer, including "0.1" and "foo".
Ah I see, I might have to go back to the drawing board then. Thank you for responding.
To determine the type of input entered when the type is an int, try:

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>

using namespace std;

int main()
{
	int n {};

	cout << "Please enter a number:  ";

	if (!(cin >> n) || (cin.peek() != '\n' && cin.peek() != '.'))
		cout << "You have not entered a number\n";
	else
		if (cin.peek() == '.')
			cout << "You have entered a float number\n";
		else
			if (n == 0)
				cout << "You entered 0\n";
			else
				if (n < 0)
					cout << "You have entered a negative number\n";
				else
					cout << "You entered: " << n << '\n';

}


However, if you only want to determine if the entered number is valid or not, then consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
	int n {};

	cout << "Please enter a number:  ";

	if (!(cin >> n) || cin.peek() != '\n' || n <= 0)
		cout << "You have not entered a valid number\n";
	else
		cout << "You entered: " << n << '\n';
}

Last edited on
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
#include <iostream>
#include <string>
#include <limits>

using namespace std;

int main()
{
 int n;
 
 do {
     cout << "Enter a number greater than 0. ";
     cin >> n;
     
     if(!cin) {
         cout << "Not valid." <<endl;
         cin.clear();
         cin.ignore(numeric_limits<streamsize>::max(), '\n');
         n = 0;
         }
     
     
 } while(n <= 0);
 
 
 return 0;
 
}


It should also be noted that you need not worry about anyone putting in a float value. It will be converted to an int automatically. If someone puts 5.8 for example it will take only the value of 5.
Last edited on
Topic archived. No new replies allowed.