Data validation

Hello I have an assignment from class that I desperately need help with

So I am to make a program that asks user to input a positive integer
e.g 1234, 234, 123456
However if the user inputs any 1 or 0 in the integer e.g 0234, 2314
the program will validate the number and
it will reject that integer every time
and ask the user to input a integer again.

I was taught in class that I would need to use to do while loop
however I now face 2 problems.

1.the while condition is too long and I can only account up to 5 digit long input,
I need another way to set the condition while account for any digit long integer.

2.integer/10%10 == 0 this particular line will reject everything even if i put in a 2,3,4 or 23456. Hence I did not bother to add in the next
integer/100%10 == 0

I cannot think of another way to reject a 0 not using this condition.

I hope someone could help me out here.


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>

using namespace std;


int main() 
{	
	int integer,temp;

	do
	{
		
		cout << "Please enter a positive integer: ";
		cin >> integer;
		
		
	
	}
	
	while( integer%10 ==1 || integer/10%10 ==1   || 
	   integer/100%10== 1 || integer/1000%10==1  || 
	   integer/10000%10==1|| integer/100000%10==1||
	    integer%10 == 0 || integer/10%10 == 0 );
	
	
return 0;	
}
Last edited on
what about represent your integer as a string and check for 0s and 1s in the string?
Sorry I'm still new to this and the professor teaching us
have not gone through string
so I do not expect him to ask us to use string.

I would try something like

1
2
3
4
5
6
do{
//update counter

//your check

} while(power(10,counter) < integer)

this way you can check all the digits up to the largest one.
Topic archived. No new replies allowed.