Don't know how to loop this properly and perhaps an infi loop problem

Hey, just started learning c++ a few days ago. How do I make my code back to the start after typing in aaa for the first input. It just starts to exit the program instead where I would like it to ask error enter an number if the user inputs aaa or just any character or letter.

I want to program to run exactly like this where when I enter a when asked "Enter the number of numbers you want to check" and I input a it doesn't end the program but goes back to the start and ask the same question. I want it to check both conditions of it not being a negative number and no letters.

http://cpp.sh/7322

http://cpp.sh/4ylvn

```
#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;



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;

{

cout << "ERROR: enter a number:" <<endl;

cin.clear();

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

}
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
duplicate, keep it in the original ongoing.
Topic archived. No new replies allowed.