I'm trying to print out whether a number is prime or not up to and including the user inputted integer. The problem I'm having is that regardless of the user inputted integer, the loop only runs up to 4. How do I break from the nested loop without ending the opening loop?

The code I'm using is below.

Many thanks.

#include <iostream>
using namespace std;

int main ()
{
    int N, a;
    loop:
    cout << "Please enter a positive integer or 0 to quit: ";
    cin >> N;
    if (N==0) return 0;
    else {
    while (N<1)
    {
        cout << "Please enter a positive integer or 0 to quit: ";
        cin >> N;
        if (N==0) return 0;
    }
    for (int j=1;j<=N;j++)
    {
         if (j==1||j==2) {cout << j << " is prime."  << endl; cout << "\n";}
         else
         {
             for (int i=2;i<j;i++)
             {
                  if (j%i==0)
                  {
                       cout << j << " is not prime.\nIt is divisible by " << i << "." << endl;
                       cout << "\n";
                       j=a;
                       break;
                  }
            }
            if (j!=a) {cout << j << " is prime." << endl; cout << "\n";}
         }
    }
    goto loop;
    }
    return 0;
}

Ignoring the questionable use of goto here, you never initialize or update the value of a, yet that value is assigned to j when a number is not prime. That raises red flags. If the purpose of this algorithm is to print the primality of every number from [1,N] then there's really no point in updating j at all.

So let's try this a different way. Use a simple boolean flag, initialized to true, and set it to false if the primality check fails. Then after that check, test the flag and display success if it's still true:

#include <iostream>
using namespace std;

int main ()
{
    int N;
loop:
    cout << "Please enter a positive integer or 0 to quit: ";
    cin >> N;
    if (N==0) return 0;
    else {
        while (N<1)
        {
            cout << "Please enter a positive integer or 0 to quit: ";
            cin >> N;
            if (N==0) return 0;
        }
        for (int j=1;j<=N;j++)
        {
            if (j==1||j==2) {cout << j << " is prime."  << endl; cout << "\n";}
            else
            {
                bool isprime = true;

                for (int i=2;i<j;i++)
                {
                    if (j%i==0)
                    {
                        cout << j << " is not prime.\nIt is divisible by " << i << "." << endl;
                        cout << "\n";
                        isprime = false;
                        break;
                    }
                }

                if (isprime) {cout << j << " is prime." << endl; cout << "\n";}
            }
        }
        goto loop;
    }
    return 0;
}

Thanks again Deceptikon. I've been staring at this for a few hours and now that you've pointed it out I can't believe how blind I've been, missing such a blatant error. /sigh
Cheers! :)

I think as programmers we have a tendency to read our own code with a mental filter and see what it should do as opposed to what it actually does. It's kind of hard to objectively read one's own code, that's why pair programming and code reviews are so effective.

This article has been dead for over six months. Start a new discussion instead.