| Author |
c++ Prime number extra credit |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
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;
}
 |
|
| Author |
RE: c++ Prime number extra credit |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
'root' is a float. Float's don't have remainders when you divide them. % is an integer operator.
|
|
| Author |
RE: c++ Prime number extra credit |
j4m32
Member
Posts: 81
Location:
Joined: 01.05.10 Rank: God |
|
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 |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
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 vegeta_man111 on 21-02-11 01:00 |
|
| Author |
RE: c++ Prime number extra credit |
JDavidC
Member
Posts: 8
Location:
Joined: 30.01.11 Rank: Elite |
|
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 |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
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
Posts: 81
Location:
Joined: 01.05.10 Rank: God |
|
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, |
|
| Author |
RE: c++ Prime number extra credit |
GTADarkDude
Member

Posts: 142
Location: The Netherlands
Joined: 23.02.08 Rank: God |
|
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
Posts: 81
Location:
Joined: 01.05.10 Rank: God |
|
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, |
|