Endless loop Problem

Write your question here.
When I input -5 it goes to an endless loop. This is after hitting aaa on the first question. Don't know how to add these two conditions without this endless loop problem. At the beginning part while (!(cin>> numberCheck))


#include <iostream>
#include <limits>
using namespace std;


int main()
{
int numberCheck=-1;
while(numberCheck<0){
cout<<"Enter the number of numbers you want to check: ";
cin>>numberCheck;

while (!(cin>> numberCheck))
{
cout << "ERROR: enter a number:";

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if(numberCheck<0)
cout<<"Wrong number. Try one more time" << endl;






}





int j;
for(j=0;j<numberCheck;j++){
//description variable:
int number=-1;

//input number:
while((number<1)||(number>1000)){
cout<<"Input a number between 1 and 1000: ";
cin>>number;
while (!(cin >> numberCheck))
{
cout << "ERROR: enter a number:";

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if((number<1)||(number>1000))
cout<<"That number is not valid. Input another number." <<endl;

}

//Determine if a number is a prime number or not.
//A prime number is one whose only exact divisors are 1
//and the number itself (such as 2, 3, 5, 7, etc.).
int i;
int nDiv=0;
int isSimple=1;
for(i=2;i<number;i++)
if(number%i==0)
{
if(isSimple==1){
cout<<"That is not a prime number. Divisors are:"<<endl;
cout<<"1\t";
nDiv=1;
isSimple=0;
}
cout<<i<<"\t";
nDiv++;
if(nDiv%10==0)
cout<<endl;
}
if(isSimple==0) //if not simple, output last item:
cout<<number<<endl;
else
cout<<"That is a prime number."<<endl;
}
return 0;
}
Last edited on
cin>>numberCheck; <-- you don't need this. the while loop is doing your cin with validation, but reading it before the loop with no validation at all is causing it to get into a bad state.
while (!(cin>> numberCheck)) if I get rid of this line at the start, how do I make it so that it loops back to the start without exiting the program? When I input aaa
Last edited on
That loop is your data validation loop (not sure if it works or not yet, but removal is not helpful).
currently it looks like the right ideas...
get valid input
process that input.
if you want to repeat that cycle, to get more input and process again, you can put a loop around the whole thing, but I would get it working ONCE before doing that part, its just visual clutter until the inner part works.

if you put in aaa, the while loop should fail and restart until you get a positive int, but again, not sure if you did it right, I didn't check that part out because the extra cin is messing it up.

unrelated but you only need to check to sqrt of a number for primes, which is significant if you start throwing numbers > a million or so at it. A number can't have a factor > its sqrt that is not paired with a second factor < its root. (or equality for perfect squares, which pairs the root with itself, of course).
Last edited on
http://cpp.sh/9tqrx

here it works, but doesn't loop back from the start. don't know how to do that
Last edited on
without checking the go again input, which you can add yourself, the basic idea is:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
 char again = 'n';
 do 
{
  //all your existing code
  cout >> "go again? >"
  cin << again; //whatever approach to validate this, eg getline into a string and check "N" or "n" 
} while(again != 'n' && again !='N');

}
Last edited on
Topic archived. No new replies allowed.