c++ Prime number extra credit
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;
}
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'
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.
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.
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,
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.