I know there are tons of posts on this, I think I'm on the right track with this, but I can't quite get it to work. I can't figure out how to get the range of primes to print. I'd appreciate any input I can get, sorry about the indenting.

main() keeps doing the following, until user enters a 0 or a negative number:
• Ask user to enter a number;
• If it is positive, print all the prime numbers between 1 and it; when there’s no prime number, print “none; ( A prime number is a whole number that can only be divided by 1 and itself, i.e., 2, 3, 5, 7, 11, 13, 17, 23, ….)
• Print the max of these prime numbers; or print “none when there’s no prime.

The calling relation should be: main() calls printPrimes() and getMaxPrime();
printPrimes() and getMaxPrime() calls isPrime() .

My Code:

#include <iostream>
#include <math.h>
using namespace std;


/* function isPrime (int num) finds out whether the input integer num is a prime number or not
*/
bool isPrime (int num)
{	
	for (int fac = 2; fac <= num; fac++)	
		if (num % fac == 0) return 0;
	return 1;	
}




/* function printPrimes (int num) prints all the prime numbers between 1 and num
*/
void printPrimes (int num)
{
	int number = 1;
	while(number <= num)
	{
		++number;
		if (isPrime(number))
			cout << number<<" ";
	}	
}




/* function getMaxPrime(int num) returns the greatest prime number between 1 and num. if there is no prime number between 1 and num, getMaxPrime() should return a 0.
*/
int getMaxPrime (int num)
{
	int maxCount = 0;
	for (int count = 1; count <= num; count++)
	{
		if (isPrime(count))
			maxCount = count;
	}		
	return int (maxCount);		
}




int main ()
{
	int number;
	
	do
	{
		cout 	<<endl<<endl 
				<<"Please enter a number, 0 or negative to stop :";
		cin >> number;
		if (number > 0)
		{
			cout << "All the prime numbers between 1 and "<< number<<" are: "<< endl;
			printPrimes(number);
			cout << "The max prime is :" ;
			int maxPrime = getMaxPrime(number);
			if (maxPrime==0) cout <<"none.";
			else cout << maxPrime;
		}
	} while (number>0);
	return 0;
}

i'm not sure if I understood the problem but try to replace

if (num % fac == 0)

with

if ((num % fac == 0) && (num != fac))

and use this

void printPrimes (int num)
{
	int number = 2;
	while(number <= num)
	{
		
		if (isPrime(number))
			cout << number<<" ";
		++number;
	}	
}

instead your printPrimes
I didn't had the time to test it

if you want between 1 and num (num not included) than

/* lab3.cpp  Working with Functions
*/

#include <iostream>
#include <math.h>
using namespace std;


/* function isPrime (int num) finds out whether the input integer num is a prime number or not.
	If num is a prime, return true; else return false;
   A prime number is a number whose only factors are 1 and itself.
	Note: 0 and 1 are not prime numbers. The minimal prime number is 2.

*/
bool isPrime (int num)
{	
	for (int fac = 2; fac <= num; fac++)	
		if ((num % fac == 0) && (num != fac))
			return 0;
	return 1;	
}




/* function printPrimes (int num) prints all the prime numbers between 1 and num
*/
void printPrimes (int num)
{
	int number = 2;
	while(number < num)
	{
		
		if (isPrime(number))
			cout << number<<" ";
		++number;
	}	
}




/* function getMaxPrime(int num) returns the greatest prime number between 1 and num.
	if there is no prime number between 1 and num, getMaxPrime() should return a 0.
*/
int getMaxPrime (int num)
{
	int maxCount = 0;
	for (int count = 1; count < num; count++)
	{
		if (isPrime(count))
			maxCount = count;
	}		
	return int (maxCount);		
}




int main ()
{
	int number;
	
	do
	{
		cout 	<<endl<<endl 
				<<"Please enter a number, 0 or negative to stop :";
		cin >> number;
		if (number > 0)
		{
			cout << "All the prime numbers between 1 and "<< number<<" are: "<< endl;
			printPrimes(number);
			cout << "The max prime is :" ;
			int maxPrime = getMaxPrime(number);
			if (maxPrime==0) cout <<"none.";
			else cout << maxPrime;
		}
	} while (number>0);
	return 0;
}

I still can't get the printPrimes function to work. It doesn't output anything. and the getMaxPrime function returns 1. does anybody have any suggestions for this?

Finally got it working. I thought I'd post the code in case someone else has the same problem

#include <iostream>

   using namespace std;


/* function isPrime (int num) finds out whether the input integer num is a prime number or not.
	If num is a prime, return true; else return false;
   A prime number is a number whose only factors are 1 and itself.
	Note: 0 and 1 are not prime numbers. The minimal prime number is 2.

*/
    bool isPrime (int num)
   {
      bool primeCheck = true;   	         
      for (int fac = 2; fac < num; fac++ )//runs through factors between 2 and num
      {	
         if (num % fac == 0) //checks to see if a number is divisible
            primeCheck = false;
      }		
      return (primeCheck);	//returns true or false to calling functions printPrimes and getMaxPrime.
   }

/* function printPrimes (int num) prints all the prime numbers between 1 and num
*/
    void printPrimes (int num)
   {
      bool print;
      if (num <= 1)
   		cout << "none!"; // Determines if their are any primes in the range. 
   		
      for (int count = 2; count <= num; count++ )
      {
         print = isPrime(count); //Creates "print" function to determine if a number is prime.
         if (print == true)
         {
            cout << count << ", "; // outputs prime number 
         }
      }	
   }
				
				
				 	   		




/* function getMaxPrime(int num) returns the greatest prime number between 1 and num.
	if there is no prime number between 1 and num, getMaxPrime() should return a 0.
*/
    int getMaxPrime (int num)
   {
      int maxPrime;
      int prime = 2;
      if (num <= 1) //sets maxPrime to "0" (none)
   	{
   		maxPrime = 0; 
   		prime = 0;
   	}
   		
      do  //checks if a number is prime, sets maxPrime equal to found primes.
      {
         	bool check = isPrime(prime);
         	if (check == true)
            	maxPrime = prime; //sets maxPrime to last prime number found in range.
         	prime ++;
         
      }while (prime <= num);	
      return (maxPrime); // returns the value of maxPrime to main function		
   }




    int main ()
   {
      int number;
   
      do
      {
         cout 	<<endl<<endl 
            <<"Please enter a number, 0 or negative to stop :";
         cin >> number;
         if (number > 0)
         {
            cout << "All the prime numbers between 1 and "<< number<<" are:"<< endl;
            printPrimes(number);
            cout << endl;
            cout << "The max prime is :" ;
            int maxPrime = getMaxPrime( number );
            if (maxPrime<=1) cout <<"none.";
            else cout << maxPrime;
         }
      } while (number>0);
      return 0;
   }

Do you know what is the definition of the prime number? It is a number that has no other diveders other than one and itself. You tested the number to itself (every number devides it self, doesn't it?) so that is why the function always return false.
If the number isn't prime it must have deviders to (inclusively) its square root. Think about it, and say why.

If you exclude number 2, and start you for loop with 3, you need to check only odd numbers, increment you for loop by 2.

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.