### Finding all prime numbers between 2 values

My assignment is to find and output all the prime numbers between two user inputted values. Also, the program is required to contain two functions with the following guidelines:

Function 1: A function that determines whether or not a particular integer is prime. This function has a single integer parameter which is pass by value and returns a Boolean value.

Function 2: A function that writes to an output file all of the prime numbers between two specified integers. This function has three parameters, the two specified integers which are pass by value and the output file object that is pass by reference. This function does not return a value.

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

//Declaring universal variables
int number1;
int number2;
int x;
string outfile = "Output.txt";
ofstream fout(outfile.c_str());

//Initializing Variables
int isprime = 0;
int showprimes = 0;

//Function Prototype
bool IsItPrime(int isprime);
void ShowPrimes(int number1,int number2,ofstream fout);

//Main function
int main()
{
//Input
cout<<"Enter the first value: ";
cin>>number1;
cout<<"Enter the first value: ";
cin>>number2;

//Call functions
IsItPrime(isprime);
ShowPrimes(number1,number2,fout);

}

//Outside function that determines if the number is prime
bool IsItPrime(int isprime)
{
for(int i=number1;i<number2;i++)
{
if(number1%i==0)
{
isprime=0;
}
else
{
isprime=1;
}

}

return isprime;
}

//Outside function that outputs the prime numbers between 2 values
void ShowPrimes(int number1,int number2,ofstream fout)
{
for(x=number1;x<=number2;x++)
{
bool IsItPrime(int isprime) = 1
fout<<x<<endl;
}
}
Last edited on
Some notice you should know:

This line:
`fout<<ShowPrimes(showprimes);`
will only write the only result ( return <something>; ) , not all of them.

 Function 1: A function that determines whether or not a particular integer is prime. This function has a single integer parameter which is pass by value and returns a Boolean value.

`bool IsItPrime(bool isprime);`
You should send another type to your function. It's stated on the question.

Another one:
 Function 2: A function that writes to an output file all of the prime numbers between two specified integers. This function has three parameters, the two specified integers which are pass by value and the output file object that is pass by reference. This function does not return a value.

- You should know what to do. It's clear that you should use 3 parameters instead of one as in your current functions.
- Also change the function type.

`for(int i=2;i<number1;i++)`
It should based on the 2 numbers from user input, not from 2 to number1. Your first function is working, but it's not working based on given condition.

I recommend also to use the screen ( cout ) to print the result. It'd be much easier to debug. When you're ready, use file.
Last edited on
Alright, updated the code above. I think I fixed all the issues you addressed. Now I'm stuck on what to actually put into the second function to have it achieve it's purpose.
Ok, the way your code is structured, you won't be able to implement what you need.

Your assignment says to "find all primes between two [natural] numbers". This to me suggests a loop:

 ``1234`` ``````// pseudo-code for all integers X in the range [ smallest, largest ] do if X is prime then output X to the file``````

You've been told to use two functions:

 ``1234567`` ``````bool is_prime( int number_to_check ) { // implement me } void output( int smallest, int largest, ofstream& output_file ) { // implement me... see pseudo-code }``````

Updated again, I don't understand how to tell the second function that the first function determined the number was prime/not prime.
You are close.

 ``123456789101112`` ``````// pseudo-code void ShowPrimes( int number1, int number2, ofstream& fout ) { for(x=number1;x<=number2;x++) { // This is wrong: bool IsItPrime(int isprime) = 1 // Something like this: if( IsItPrime( x ) ) fout<

Topic archived. No new replies allowed.