Help with my c++ code

I assume you are new to this, but please try to follow what I say.

I don't know how you came up with this algorithm, but that is definitely not how you check if a number is prime.

  • You don't need an array at all. If you wanted to search whether the input is a prime number by checking whether it exists in the array, you'd need a pretty big array. Typing out millions of prime numbers is, well, not a good idea.
  • Instead, you should do some operation on the number itself that will tell you whether it is prime.
  • Let's look at the basic definition of prime: A number is prime if it's only factors are 1 and itself.
  • Which means if you loop from 1 to the number and keep track of the factors you found, and the number of factors you see is 2, it is prime; if there are more than 2 factors it is not prime.
  • This, I hope, gives you some idea how to proceed. I would suggest you rewrite the function.
  • Note that this process can be further optimized, but this is the most intuitive way.

Coming to your code,

  • Well there is only one return statement, and it says return 0; so the function naturally always returns 0.
  • The function is supposed to return a bool which means it should return true or false, not 0.
  • What is the purpose of the variables NotPrime and PrimeCheck? You don't you use them anywhere.
/r/learnprogramming Thread