### 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?
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 #include #include using namespace std; int checkprime(int n) { int i; int prime=1; for(i=2;i> b; cout << "**************************************************" <1000) { cout << "Please enter an integer that is within the range." <> input; cout << "**************************************************"; cout << "\nEnter number: "; cin >> b; cout << "**************************************************" <> 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:

 1234567891011121314 if (b<0|| b>1000) { cout << "Please enter an integer that is within the range." <> input; cout << "**************************************************"; cout << "\nEnter number: "; cin >> b; cout << "**************************************************" <

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:

 1234567891011121314 while(b<0|| b>1000) { cout << "Please enter an integer that is within the range." <> input; cout << "**************************************************"; cout << "\nEnter number: "; cin >> b; cout << "**************************************************" <

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.
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 #include #include using namespace std; int checkprime(int n) { int i; int prime=1; for(i=2;i> b; cout << "**************************************************" <1000) { cout << "Please enter an integer that is within the range." <> input; // cout << "**************************************************"; // cout << "\nEnter number: "; // cin >> b; // cout << "**************************************************" <> 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.