Modulus Operator Problem

Ok, I've narrowed my issue down to the "modulus," not the "for ()" loop. I'm trying to make a program to have the computer make a list of prime numbers. If I substitute 3 into int "tester," next to the "remainder" equation, it works fine. Why can't I use a variable in this loop "int remainder=x % tester?"

Be CAREFUL, this app will crash, but should compile fine.




#include <iostream>

using namespace std;

bool isitprime(int x)
{
for (int tester=0; tester<x; tester++)
{
int remainder=x % tester;
if (remainder==0)
{
cout<<"yea";
return 1;
break;
}
}
return 0;
}

int main()
{
int y;
int q;
cin>>y;
cout<<isitprime(y);

}
What happens when you divide something by zero?
Division by zero int remainder=x % tester; will cause an exception.

In terms of the algorithm, tester could start from 2, not 0.
OH! it approaches infinity :P? Jokes, but thank you. Can't divide 1/0, duh... I need to make tester=2 then... makes sense.

I see, these little things, you just wouldn't happen to think of are important. I feel stupid and am new to this c++ stuff...
Last edited on
Topic archived. No new replies allowed.