Check Failed

Why does that happen? It checks in the first try that it is not between the valid integers but when i redo the test it skips the check?
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <string>
using namespace std;


int checkprime(int n)
{
	int i;
	int prime=1;
	for(i=2;i<n/2;i++)
	{
	if(n%i==0)
		{
		prime=0;
		break;
		}
	}
	return prime;
}
	
int isperfect(int x)
{
	int sum=0;
	int i;
	for(i=1;i<x;i++)
	{
		if(x%i==0)
		{
			sum=sum+i;
		}
	}
	
	if(sum==x)
	{
	return 1;
	}
	else
		{
		return 0;
		}
}


	
int main()
{
	
	
	string input;
	do

{
	int b;
	int i;
	int prime=1;
	cout << "**************************************************" <<endl;
	cout << "Enter number: ";
	cin >> b;
	cout << "**************************************************" <<endl;






	if (b<0|| b>1000)
	 
			
				{
				cout << "Please enter an integer that is within the range." <<endl;
				cout <<"**************************************************" <<endl;
				cout << "Do you want to quit?" << endl 
				<< "[Y]es [N]o: " ;
				cin >> input;
				cout << "**************************************************";
				cout << "\nEnter number: ";
				cin >> b;
				cout << "**************************************************" <<endl;
				}
	





		if(isperfect(b)==1)
	{
	cout << b << " is a perfect number. \n";
	}
	
	else
	{
	cout << b << " is not a perfect number. \n";
	}
	
	
	
		if(checkprime(b)==1)
	{
	cout << b << " is a prime number. \n";
	}
	
	else
	{
	cout << b << " is not a prime number. \n";
	}
											

	{ 
		prime=0;
		cout << "Divisors of "<< b << ":" << " ";

		for(i=2;i<b;i++)
		if(b%i==0)
	{ 
		cout << i << " ";
		prime=1;
	}
		b++;
	}
	
	cout <<"\n**************************************************" <<endl;
	cout << "Do you want to quit?" << endl 
		<< "[Y]es [N]o: " ;
	cin >> input;
}	while ((input == "N") || (input == "n")); 

	return 0;
}


**************************************************
Enter number: 1212
**************************************************
Please enter an integer that is within the range.
**************************************************
Do you want to quit?
[Y]es [N]o: n
**************************************************
Enter number: 1212
**************************************************
1212 is not a perfect number.
1212 is not a prime number.
Divisors of 1212: 2 3 4 6 12 101 202 303 404 606
**************************************************
Do you want to quit?
[Y]es [N]o:
EDIT: It would be a good programming practice if you remove the extra spaces in your program and line up the curly braces right, it helps us read the code easier so we dont need to scroll through 20-30 arbitrary lines of spaces.

Your error check doesnt check the value after you initially check it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (b<0|| b>1000)
	 
			
				{
				cout << "Please enter an integer that is within the range." <<endl;
				cout <<"**************************************************" <<endl;
				cout << "Do you want to quit?" << endl 
				<< "[Y]es [N]o: " ;
				cin >> input;
				cout << "**************************************************";
				cout << "\nEnter number: ";
				cin >> b;
				cout << "**************************************************" <<endl;
				}


once you enter b for the second time it doesnt check if b is within valid range again, it just continues on with the loop. You would need to check the code to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(b<0|| b>1000)
	 
			
				{
				cout << "Please enter an integer that is within the range." <<endl;
				cout <<"**************************************************" <<endl;
				cout << "Do you want to quit?" << endl 
				<< "[Y]es [N]o: " ;
				cin >> input;
				cout << "**************************************************";
				cout << "\nEnter number: ";
				cin >> b;
				cout << "**************************************************" <<endl;
				}


so that WHILE b is less than zero or greater than 1000, it will continue to ask for valid input.
Last edited on
why does it not exit?
**************************************************
Enter number: 1005
**************************************************
Please enter an integer that is within the range.
**************************************************
Do you want to quit?
[Y]es [N]o: y
**************************************************
Enter number:
Where do you check what the user says? I don't see any code where you handle the users input with y or n.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
using namespace std;


int checkprime(int n)
{
	int i;
	int prime=1;
	for(i=2;i<n/2;i++)
	{
	if(n%i==0)
		{
		prime=0;
		break;
		}
	}
	return prime;
}
	
int isperfect(int x)
{
	int sum=0;
	int i;
	for(i=1;i<x;i++)
	{
		if(x%i==0)
		{
			sum=sum+i;
		}
	}
	
	if(sum==x)
	{
	return 1;
	}
	else
		{
		return 0;
		}
}


	
int main()
{
	
	
	string input;
	do{
		int b;
		int i;
		int prime=1;
		cout << "**************************************************" <<endl;
		cout << "Enter number: ";
		cin >> b;
		cout << "**************************************************" <<endl;


	if(b<0|| b>1000)
		{
			cout << "Please enter an integer that is within the range." <<endl;
			cout <<"**************************************************" <<endl;
// 			cout << "Do you want to quit?" << endl 
// 			<< "[Y]es [N]o: " ;
// 			cin >> input;
// 			cout << "**************************************************";
// 			cout << "\nEnter number: ";
// 			cin >> b;
// 			cout << "**************************************************" <<endl;
 		}

    else{                 //add
			if(isperfect(b)==1)
			{
			cout << b << " is a perfect number. \n";
			}
	
			else
			{
			cout << b << " is not a perfect number. \n";
			}

			if(checkprime(b)==1)
			{
			cout << b << " is a prime number. \n";
			}
	
			else
			{
			cout << b << " is not a prime number. \n";
			}
											

			{ 
				prime=0;
				cout << "Divisors of "<< b << ":" << " ";

				for(i=2;i<b;i++)
				if(b%i==0)
				{ 
				cout << i << " ";
				prime=1;
				}
				b++;
			}
		}

		//handle the users input with y or n. 
		cout <<"\n**************************************************" <<endl;
		cout << "Do you want to quit?" << endl 
			<< "[Y]es [N]o: " ;
		cin >> input;
	}while ((input == "N") || (input == "n")); 

	return 0;
}
You change the if (b<0|| b>1000) to while(b<0|| b>1000)
it will be a Endless loop

Topic archived. No new replies allowed.