Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

c++ Prime number extra credit


ghost's Avatar
0 0

having a error from line 33 (if statement). says 'invalid operands of types 'float' and 'int' to binary 'operator%'


// Chapter X Assignment X
// Corey Hartshorn

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main ()
{
    unsigned long num = 3;
    int count;
    int divisor = 3;
    int find = 2;
    float root;
    
    
    
    cout <<"How many primes?:" << endl;
    cin >> count;
    system("pause");
    cout << "2" << endl;
    
    do
    {
         cout << num << endl;
         find++;
         root = sqrt(num);
         
         while (divisor <= root)
         {
               if (root % divisor == 1)
               {
               divisor = divisor + 2;
               }
               else break;
         
         }
         
    } while (find <= count);
    
    
    system("pause");
    return 0;
}



ynori7's Avatar
Future Emperor of Earth
0 0

'root' is a float. Float's don't have remainders when you divide them. % is an integer operator.


j4m32's Avatar
Member
0 0

You're not ofay with type conversion or casting? Okay.

On the surface it looks like all you really need to do is to cast root as as an integer (may not be ideal) Although I don't know if this introduces errors.

Jim,


ghost's Avatar
0 0

i was figuring that but i dont know how to make root a int without messing the formula. I could see it being a problem when I get to bigger numbers in the 100,000 area with that root being less by 1. Do you think it will affect it?

edit: fixed it, but now i am having problems with:

root = sqrt(num);

error says '[Warning] converting from 'int' to 'double'


JDavidC's Avatar
Member
0 0

Take a look at how the sqrt function is defined, and what explicit casting vs implicit casting is. Use Google if you need help with what stuff in my first sentence is. What is happening now won't stop the program from compiling unless the compiler is set to treat warning messages as errors. I would advise you to find the cause of the warning and to eliminate it (it is good practice to try to eliminate these). I can't really say more without spoiling how to deal with that problem.


ghost's Avatar
0 0

DevC++ will not let me compile with errors. I looked up explicit casting and implicit casting and get what you are saying a little. I am still a little lost and any info you have to help me is greatly appreciated. Thanks for not just telling me how to fix it and actually helping me do it too. I like to learn how to do it myself, instead of fixing what others say is wrong and not knowing why.


j4m32's Avatar
Member
0 0

You do know DevC++ is an IDE not a complier, right? MinGW (Windows port of GCC) is the compiler you're using when you tell "DevC++" to compile your code.

I don't think any compiled language (at least, that I know of) will complete the linking stage and produce an executable binary if there are ANY syyntax / semantical etc errors at the compile/assemble stage beforehand, so what do you expect? :)

Jim,


GTADarkDude's Avatar
Member
0 0

vegata_man, please just take a look at this. You can see that sqrt accepts three different variable types: double, float and long double. It it not necessary that you understand all differences between them, but do keep in mind that every variable (and function) is of a certain type. When the compiler expects a variable to be of a certain type, because the function is only defined for that certain type for example, it can give an error when it encounters a variable of a different type. In some cases, the compiler can automatically convert this type to the type that is expected. This converting is called typecasting. When the compiler cannot do this automatically for you and gives an error, you have to explicitly tell him to change the type. This page tells you about some ways to manage this.

Some random remarks on your code:

  • As said in the other thread, try to stay away from system("pause"), because pause is a Windows-only command and you should prefer to keep your code platform independent.
  • Usually, you should use doubles in stead of floats. Doubles are more precise, and the few extra bytes shouldn't really be an issue.

I don't think any compiled language (at least, that I know of) will complete the linking stage and produce an executable binary if there are ANY syyntax / semantical etc errors at the compile/assemble stage beforehand, so what do you expect?Actually, I think javac does. NetBeans sometimes tells me that one or more classes were compiled with errors, but then it gives me the possibility to run it anyway. Don't really know what precisely happens though. Usually I fix my errors before I actually run the program.

EDIT @ j4m32: ok, thanks. Didn't know that.


j4m32's Avatar
Member
0 0

For Java, if there is a syntax or semantic error upon recompiling, it does not delete the previously compiled class file. So you'll just be running the previous code.

Jim,