I am currently in the process of learning C++ (I started about a week ago), and the book I have been reading (Jumping into C++) asked me to create a code of which displays all numbers from 0-100, but only if their prime factors add up to 7. So I have spent 4 days trying to figure out how to do this, and I have still been unsuccessful. I have gotten a fair way into it, but have yet to get it working (I use GCC compiler for Windows, if that is important or something). If you could help, that would be greatly appreciated. But please, since I was given this task as a test to make sure I understand all about "figuring out the algorithm", please, do not tell me exactly what to do to get it going, but if you could answer some questions or *hint* at where to go next, thanks.
My current questions are:
1) How can I clean up this code a bit more, it seems a bit messy right now to me.
2) For each of my "if" statements in the "main" function, excluding the first one, I get an error saying "IOS C++ forbids comparison between a pointer and an integer". When I googled this error, I found results where people wrote a "string" instead of a "char"...I did not do either. Help?
3) If you think hinting where to go won't ruin my thing about having to figure it out myself, hint just one thing please?
My code (sorry it's so large! Can't find "spoiler's" or anything to make it smaller):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
using namespace std;
bool isDivisible (int number, int divisor);
bool notPrime (int number);
int factor_test = 2;
int prime_factor1;
int prime_factor2;
int main()
{
for ( int i = 1; i < 100; i++ )
{
if ( notPrime( i ) )
{
if ( i % factor_test == 0 && i / factor_test == notPrime )
{
switch ( factor_test )
{
default:
factor_test == prime_factor1;
if ( i % prime_factor1 == 0 && i / factor_test == notPrime )
{
switch ( factor_test )
{
default:
prime_factor1 == prime_factor2;
}
}
}
}
else
{
factor_test++;
if ( i % factor_test == 0 && i / factor_test == notPrime )
{
factor_test == prime_factor2;
}
}
}
else
{
return false;
}
}
}
bool notPrime (int number)
{
for ( int j = 2; j < number; j++)
{
if ( isDivisible( number, j ) )
{
return true;
}
else
{
return false;
}
}
}
bool isDivisible (int number, int divisor)
{
return number % divisor != 0;
}
|