HELP ON INVALID OPERANTS!!!

ilovepie10
2011-12-25
2012-09-26
  • ilovepie10

    ilovepie10 - 2011-12-25

    This is my program:

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main() {
        double n, i;   // Declaring variable i and n 
        bool is_prime = true; // Boolean flag; assume true until proven otherwise
    
        cout << "Enter a number and press ENTER: ";
        cin >> n;
    
        for (i = 2.0; i <= sqrt(n); i++)    // Test for prime by checking for divisibility by all whole numbers from 2 to sqrt(n).
    
        {if (n % i == 0.0) 
        bool is_prime = false;}
    
        if (is_prime = true) {
            cout << "Number is prime." << endl;
        }
        else {
            cout << "Number is not prime." << endl;
        }
        system("PAUSE");
        return 0;
        }
    

    It is to show weather a number is prime or not.

    On line 14:

    {if (n % i == 0.0)
    

    it says it is an invalid operant to types 'double and 'double'.

    Please help, do I have to declare i or n as a float, int, long float, long
    int, or long double? DAMNIT!!!

     
  • Jim Pattee

    Jim Pattee - 2011-12-25

    You can't use the modulo operator '%' on doubles, it can only be used on
    integers.

     
  • Jim Pattee

    Jim Pattee - 2011-12-25

    Take a look at fmod from the math library.

     
  • ilovepie10

    ilovepie10 - 2011-12-25

    So do I have to include fmod?

     
  • ilovepie10

    ilovepie10 - 2011-12-25

    Dev-C++ won't let me declared n as an int though, so what do I do?

     
  • ilovepie10

    ilovepie10 - 2011-12-25

    Thanks jimp03, I changed the code tho this:

    #include <iostream>
    #include <math.h>
    #include <cmath>
    using namespace std;
    
    int main() {
        double n, i;   // Declaring variable i and n 
        bool is_prime = true; // Boolean flag; assume true until proven otherwise
    
        cout << "Enter a number and press ENTER: ";
        cin >> n;
    
        for (i = 2.0; i <= sqrt(n); i++);    // Test for prime by checking for divisibility by all whole numbers from 2 to sqrt(n).
    
        if fmod(n/i) == 0; { 
        is_prime = false;
        }
    
        if (is_prime = true) {
            cout << "Number is prime." << endl;
        }
        else {
            cout << "Number is not prime." << endl;
        }
        system("PAUSE");
        return 0;
        }
    

    to have line 15

    if fmod(n/i) == 0; {
    [code]
    have a parse error before '(' token, so now its easier to fix, though I still don't know how. Please help!
    
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks