Hello I'm trying to create a program that reads a number(which is the amount of pairs) then you input the pairs and then for each pair it prints the prime numbers between the 2 numbers of the pair.Here's the code:

#include <iostream>
using namespace std;

class Prime {
	int x,y;
	public:
	void count(int,int);
};


  void Prime::count(int k,int l)
   {
	   int f=k%2;
	   int g=k%3;
	   int h=k%5;
	   do
	   {
		   if  (f==0)
           break;
           else if (g==0)
           break;
           else if (h==0)
           cout  <<  k;
           k++;
	   } while (k<=l);
}	 
int main(void)
{
	int * primes;
	int n,i;
	int a,b;
	cout << "Give  the amount of numbers you wish to try:"<< endl;
    cin  >> n;
    primes = new int[n];
    for (i=0;i<=(n-1);i++)
    {
    cout << "Give the 2 numbers:" << endl;
    cin >> a >> b;
    Prime primes[i];
    primes[i].count(a,b);
}
}

unfortunately after inputting the pairs the program stops.Any advice?Thanks in advance!

Recommended Answers

All 2 Replies

break; on 19 and 21 is taking you out of the loop completely, what you want to do is increment k and continue; . Also, you employ something like http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes because you're only testing for 3 factors.

Your primality tester is flawed, for example 49 according to this primitive test is prime (but for every number <49 it is correct). Also it will not finish if k>0, because only manipulation you do with k is incrementation in line 24, and in the following line you test it if it is less then 2.
PS. Good indention for Prime::count:

void Prime::count(int k,int l)
   {
	   int f=k%2;
	   int g=k%3;
	   int h=k%5;
	   do
	   {
	   if  (f==0)
                break;
           else if (g==0)
                break;
           else if (h==0)
                cout  <<  k;
           k++;
	   } while (k<=l);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.