Input Validation for only positive integers C++

In summary, the person is having trouble with their program accepting decimals or fractions when calculating the factorial of a number. They are unsure how to change the data type to float and use input validation to declare it as invalid. They have tried using isdigit() but it did not work. They were able to resolve the issue by using cin.fail() and cin.peek() in their program.
  • #1
USN2ENG
108
0

Homework Statement


I created a program that will calculate the factorial of the number entered and am having a hard time getting it to not accept decimals or fractions.
Code:
#include <iostream>
using namespace std;
int main (){
	int q=0;
	int number = 0;
		
	cout<<"Please enter a positive whole number:"<<endl;
	/*cin>>number;*/
	while(!(cin>>number)||number<0){
		cin.clear();
		while(cin.get()!='\n'){ [B]/*Also, this was the only thing I could find to stop it from looping Invalid input endlessly but I am not sure exactly what this is doing, if anyone could explain that would help alot*/[/B]
			continue;
			cin>>number;
		}
		cout<<"Invalid Input"<<endl;
		}
	int x = number;	
	int y=1;
	while(x>1){
		y = y*x;
		x--;
		}
	cout<<y;
	return 0;
}

The Attempt at a Solution



I am 2 months into my first programming class ever and I can't seem to figure out how to get around the fact that if I enter '3.2' or '3/2' it will only look at the '3' and not the '.2', or '/2'. I would probably need to change the data type to float or something but then I am not sure how to use the input validation after that. I tried using isdigit(number) but that was not working either.
I have googled just about everything I could think of but I am not sure where to go. If anyone can point me in the right direction that would help immensely! Thanks!
Chuck
 
Physics news on Phys.org
  • #2
I'm not sure what your problem is. Your input variable is number, which is type int. When your program prompts you to enter a positive integer, if you type 3.2, the program will accept only the integer part, not the decimal portion. Isn't this what you want?

Also, if you type in 3/2, your haven't written your program to parse arithmetic expressions such as 3/2 or 5+ 7 and so on.
 
  • #3
I wanted it to see that the decimal was there and then declare it invalid. Basically I wanted it to see if anything other than an integer is there, it is then invalid.

I was able to do it with:

Code:
 cout<<"Please enter a positive whole number:"<<endl;
	
	while((cin>>number).fail()||number<0 ||cin.peek() != '\n'){
		cin.clear();
		while(cin.get()!='\n'){
			continue;
			cin>>number;
		}
		cout<<"Invalid Input"<<endl;
		}

Thanks for all the help!
 
Last edited:

Related to Input Validation for only positive integers C++

1. What is input validation for only positive integers in C++?

Input validation for only positive integers in C++ is the process of checking user input to ensure that it is a positive integer, meaning a whole number greater than zero. This is important in programming to prevent errors and ensure the program runs smoothly.

2. How can I validate user input for only positive integers in C++?

To validate user input for only positive integers in C++, you can use the "cin" function to read in user input and then use a combination of conditional statements and loops to check if the input is a positive integer. If the input is not a positive integer, you can prompt the user to enter a valid input.

3. Why is input validation for only positive integers important in programming?

Input validation for only positive integers is important in programming because it helps prevent errors and ensures that the program runs as intended. Without proper input validation, the program may receive incorrect or unexpected input, leading to bugs and malfunctions.

4. How can I handle errors in input validation for only positive integers in C++?

If the user enters invalid input while attempting to validate for only positive integers in C++, you can use the "throw" function to throw an exception and handle the error. You can also use try-catch blocks to handle any potential errors that may occur during input validation.

5. Are there any built-in functions in C++ for input validation of positive integers?

Yes, there are built-in functions in C++ that can be used for input validation of positive integers. These include "isdigit()" to check if a character is a digit, "atoi()" to convert a string to an integer, and "cin.fail()" to check for input failure. These functions can be used in combination with conditional statements and loops to validate user input for only positive integers.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
927
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top