Follow us on Twitter!
Hacking isn't just Computers & Exploits. It's a Philosophy. - Mr_Cheese
Thursday, April 17, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 13
Guests Online: 12
Members Online: 1

Registered Members: 82822
Newest Member: TheBunter
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

c++ Prime number extra credit


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-02-11 21:01
having a error from line 33 (if statement). says 'invalid operands of types 'float' and 'int' to binary 'operator%'

Code


// 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;
}








Author

RE: c++ Prime number extra credit

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 20-02-11 21:16
'root' is a float. Float's don't have remainders when you divide them. % is an integer operator.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: c++ Prime number extra credit

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 20-02-11 21:20
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,
Author

RE: c++ Prime number extra credit


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-02-11 21:23
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'




Edited by on 21-02-11 01:00
Author

RE: c++ Prime number extra credit

JDavidC
Member

Your avatar

Posts: 8
Location:
Joined: 30.01.11
Rank:
Guest
Posted on 21-02-11 02:21
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.

Edited by JDavidC on 21-02-11 02:22
Author

RE: c++ Prime number extra credit


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 22-02-11 06:10
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.


Author

RE: c++ Prime number extra credit

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 22-02-11 08:32
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? Smile

Jim,
Author

RE: c++ Prime number extra credit

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 22-02-11 16:49
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.


...

Edited by GTADarkDude on 23-02-11 22:09
- - -
Author

RE: c++ Prime number extra credit

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 23-02-11 18:10
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,