views:

142

answers:

6

Hi there i'm trying to make a function in C++ that takes a number, i, and decides if it is a prime number or not by running through a loop to find it's multiples, and then makes sure it isn't prime through a series of test. However, it seems the loop isn't even being run through. I've told it to output no matter where in the loop it is, but I get no outputs. Here is the code:

#include <iostream>

using namespace std;

int main()
{
    int j =1;
    int z = 0;
    int i = 10;
    bool p = false;
    while (p = false){
        cout << "not starting ifs";
        z=i%j;
        if (z==0 && j>2){
        p=true;
        cout << "not prime" << endl << "loops to if";
        }
        else if (j==1){
            j++;
            cout <<"loops to else if 1";
            }
        else if ( i==2 || j==i ){
          p = true;
          cout << "prime" << endl << "loops to else if 2";
            }
            else {
            j++;
            cout << "loops to else";
                }
        }
            return 0;
}

I don't care whether or not the math behind it is right, I want to figure that out myself for the learning experience. But if anyone could help me figure this out with a good easy to understand explanation I would appreciate it! I"m really new to programming, so I'm not used to jargon yet. I look forward to your advice!

+3  A: 
while (p = false) // obvious error ...

If you don't find it : it's == not = that you want.

Klaim
+10  A: 

You are using = instead of == in

while (p = false){

When you do that, you assign false to p and the result of the expression is false which gets tested in the while loop resulting in exiting of the loop.

codaddict
Alright! That fixed it! Thanks so much!!!!
Samuraisoulification
Samuraisoulification - don't forget to accept whichever answer you went with!
ianhales
Also thanks for the explanation!!
Samuraisoulification
I will, but currently it says I have to wait 10 minutes.
Samuraisoulification
+3  A: 

change it to while (false == p)

Basically here is what happens in order:

  1. p is assigned false
  2. p is converted to bool

When you says (p = false), 'p' is assigned the value 'false'. After this, the boolean condition of the while loop tests for the value of 'p' which is now false, and the loop is never entered.

Chubsdad
+3  A: 

Since your compiler doesn't seem to be warning you about it, you should either find out how to turn on the warning, or learn the habit of using "Yoda-conditions":

while (false = p)

will cause a compile error.

Christoffer Hammarström
yoda conditions are terrible from a readability point of view. Much better here is `while (!p)`.
Alexandre C.
@Alexandre: Fair enough for booleans, but in general my point stands. If you can't get the compiler to warn you, you should program defensively.
Christoffer Hammarström
Okay, I'll go learn on them! Thanks for the tip!
Samuraisoulification
@Alexandre: `while (not p)` is more readable again :-). Figure 24.3 in the Standard.
Tony
+3  A: 

You need to change the while (p = false) to while (p == false).

Explanation: In C/C++, = assigns a property, whereas == compares values. What you are basically doing here is assigning false to p. The while loop then checks the value of the expression, which is p, which equals false, so doesn't run.

ianhales
+8  A: 

Better yet,

while(!p)

After all, that's what the operator is for.

Anonymous Coward