Okay, so I have this program here that basically lets you input a number and tells you if it's prime, or not. Every time, I get my critical error message that I built. I was brainstorming the code and got some of it fixed. Help?
floating point numbers are not precise. A floating point number minus an int will almost never be equal to 0, and it gets even less likely that it will be equal to 0.0 . Use modulus (%) to determine if there is no remainder.
#include <iostream>
usingnamespace std;
void error(){
cout << "Error. Repeat # with no decimal and no characters. No 1 allowed";
}
int main(){
while(true){
cout << "Welcome, please enter a number to check for primity: ";
int enter;
bool prime = false;
cin >> enter;
if (enter<=1) error();
for (int i=1; i<enter/2; i++)
if (enter%i == 0){
prime = true;
break;
}
if (prime) cout << "/n" << enter << " is a prime number!";
else cout << "/n" << enter << " is NOT prime.";
}
}
As a recommendation, get rid of the "goto" and replace it with a while(true) or something. Goto is bad practice that creates spaghetti code.