black hole sun
New member
Hi guys, I am writing a simple program to print out 100 prime numbers in C++ (and no, I'm not doing 100 cout's
). This is what I have so far, but for some unfathomable reason, it isn't working right. It compiles, but try to run it and you'll see what I mean.
Please, if you want to help, don't rewrite my program. just tell me what is wrong with my implementation, it is probably very simple but I just dont see the problem, and my code is well commented and indented. Thanks for any help.
Just for reference, this below code is something I worked on with a fellow classmate of mine that determined whether or not an inputted number was prime. The above code is derived from this code, which works, and because it is a derived work I dont want to deviate too far from the original.
I am a C++ newbie as you can tell, so go easy on me and don't do something wacky like put a pointer or array in there thanks

Please, if you want to help, don't rewrite my program. just tell me what is wrong with my implementation, it is probably very simple but I just dont see the problem, and my code is well commented and indented. Thanks for any help.
Code:
//prints the first 100 prime numbers
// FIXME: for some reason this doesn't work. And I cant see why not...
#include <iostream>
using namespace std;
bool isPrime(const unsigned int);
int main(void)
{
// start printing prime numbers from three
int testNum = 3;
cout << "I will now output, for your viewing pleasure, 100 prime numbers!" << endl;
cout << "2, "; // manually print 2, the first prime number
// i is only incremented if testNum really is prime
for(int i = 1; i <= 100; )
{
bool j = isPrime(testNum);
// if isPrime returns true...
if(j == true)
{
cout << testNum << ", ";
testNum++;
i++;
}
}
return 0;
}
bool isPrime(const unsigned int num)
{
int dividedBy = num;
// Assume the number is prime; therefore all I need to do
// is test for non-prime characteristics, like if the number is even
// or divisble by something.
while(dividedBy > 2)
{
// first, check if the number is even
if(num % 2 == 0)
return false;
// now, check if the number can be divided by anything,
// because if the % returns 0,
// then the number is NOT prime
dividedBy--;
int test = num % dividedBy;
if(test == 0)
return false;
}
// if we get this far without returning false, then
// the number must be prime
return true;
}
Just for reference, this below code is something I worked on with a fellow classmate of mine that determined whether or not an inputted number was prime. The above code is derived from this code, which works, and because it is a derived work I dont want to deviate too far from the original.
Code:
// asks for inputted number, then determines if the number is
// prime or or not prime.
#include <iostream>
using namespace std;
void getPrime(const unsigned int);
bool PRIME = true; //assume true
int main(void)
{
start: unsigned int number;
cout << "Please input a number and I'll tell you if it's prime." << endl;
cin >> number;
getPrime(number);
if (PRIME == true)
cout << "Prime!" << endl;
cout << "Look at another one?" << endl;
char goAgain;
cin >> goAgain;
if(goAgain == 'y' || goAgain == 'Y')
{
// reset PRIME before we go again
PRIME = true;
goto start;
}
return 0;
}
void getPrime(const unsigned int num)
{
// first, check if 0, 1, or 2 was entered
if(num == 0 || num == 1)
{
cout << "Not prime.";
return;
}
else if (num == 2)
{
cout << "Prime!" << endl;
return;
}
int div = num;
// Since PRIME is assumed true, all we do in this
// while() is test and make sure the number is indeed prime
while(div > 2)
{
if(num % 2 == 0)
{
cout << "Not prime." << endl;
PRIME = false;
return;
}
div--;
int test = num % div;
if(test == 0)
{
PRIME = false;
cout << "Not prime." << endl;
return;
}
}
}
I am a C++ newbie as you can tell, so go easy on me and don't do something wacky like put a pointer or array in there thanks

Last edited: