This is my prompt. Write a program that asks the user how many twin primes the user wants to find, reads in that goal, and then successively examines the numbers starting at 2 to see if the number is a twin prime. The program stops when the specified number of twin primes has been found. It then prints out the twin primes found.

I've searched this forum and I found a lot of stuff on twin prime numbers up to 100, but I can't figure out how to make it defined by the user. It's been years since I've done C++ so I've forgotten a few things, but I 'borrowed' the loops from cgcgames, a user here.
Here's what I have:

#include <iostream>
#include <math.h>

using namespace std;

int main () {
	int testnumber = 3; //declares the number that will be tested on. this is the first prime
	int findingprime = testnumber - 1; // the number that is used to divide by testnumber to check if there is a remainder
	int numberoftwins; // number of twin prime numbers

	cout << "Twin Prime Numbers \n";
	cout << "---------------------------------------------\n";
	cout << "How many twin prime numbers would you like to find?" << endl;
	cin >> numberoftwins;

int number=numberoftwins*2; // sets number to twin values(doubles)

	while (testnumber < 100 how do I make this defined by the user?) { //loops while primes are under 100
		if (testnumber%findingprime == 0) { // check for to see if there is no remainder and if there is its not a prime number and adds 2 the testnumber to start testing the next number
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else { //else if there is a remainder it takes one of the finding prime variable and reruns the loop
			findingprime = findingprime - 1;
		}
		while (findingprime == 1) { //if it goes through all the looping and finding prime is equal to 1 then that means testnumber is a prime
			cout << testnumber << "\n"; //print the prime to the screen
			
			testnumber = testnumber + 2;//moves onto next number
			findingprime = testnumber - 1; // sets findingprime to one lest then testnumber
		}
	}
	cout << "jhijhgijfhg" << endl; // this line has no purpose
	cin.get(); // waits for user to press enter to stop the program.

Ignoring the "prime number" aspect of the question and zoning in on the part you seem to be stuck on...

while (testnumber < 100 how do I make this defined by the user?)

if you don't want 100 hard-coded and instead want it as a user entry, create a variable called "max" and replace "100" with "max" in the code.

while (testnumber < max)

Now add some code that DECLARES max and allows the user to set it.

int max;
cout << "Enter the max : ";
cin >> max;

Putting it all together...

int max;
cout << "Enter the max : ";
cin >> max;
while (testnumber < max)

will replace this...

while (testnumber < 100)

Wow you made that look silly easy. However, I doesn't work right in this sense. Here is what my program spits out.

Twin Prime Numbers
---------------------------------------------
How many twin prime numbers would you like to find?
7
Please enter the range of the maximum value of numbers you would like to find.
10
3,
5,
7,
The first 14 are the numbers you are asking for.

So, in this sense, I didn't answer the prompt correctly because I'm not defining how many twin prime numbers, I'm defining the range of possibilities they could be under.

Here is my code.

#include <iostream>
#include <math.h>

using namespace std;

int main () {
	int testnumber = 3; //declares the number that will be tested on. this is the first prime
	int findingprime = testnumber - 1; // the number that is used to divide by testnumber to check if there is a remainder
	int numberoftwins; // number of twin prime numbers
	int max; // number declared by user of how far to look

	cout << "Twin Prime Numbers \n";
	cout << "---------------------------------------------\n";
	cout << "How many twin prime numbers would you like to find?" << endl;
	cin >> numberoftwins;
    cout << "Please enter the range of the maximum value of numbers you would like to find. \n";
    cin >> max;


	while (testnumber < max) {
		if (testnumber%findingprime == 0) { // checks to see if there is no remainder
			//and if there is its not a prime number and adds 2 the testnumber to start testing the next number
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else { //if there is a remainder it takes one of the findingprime variable and reruns the loop
			findingprime = findingprime - 1;
		}
		while (findingprime == 1) { //if it goes through all the looping and finding prime is equal to 1 then that means testnumber is a prime
			cout << testnumber << "," << endl; //print the prime to the screen
			testnumber = testnumber + 2;//moves onto next number
			findingprime = testnumber - 1; // sets findingprime to one lest then testnumber
		}
	}
	cout << "The first " << numberoftwins * 2 << " are the numbers you are asking for.\n";
	cin.get(); // waits for user to press enter to stop the program.
}

Just curious, how do I keep the colors and such in the fonts from the code?

The way I would go about this is to use a prime number function. Then use a while loop to keep track of how many sets of primes you have found

bool isPrime(int number)
{
    // put code here to check if a number is prime;
}

int main()
{
    int numberOfSets;
    int SetsFound = 0;
    int primeNumber = 3;
    cout << "How many twins do you want to find: ";
    cin >> numberOfSets;
    while (setsFound < numberOfSets)
    {
        if (isPrime(primeNumber) && isPrime(primeNumber + 2))
        {
            cout << "(" << primeNumber << ", " << primeNumber + 2 << ")\n";
            primNumber += 2
            setsFound++;
        }
    }
    return 0;
}

The font gets converted to how the bbcode is set up. I don't believe there is a way to change that.

You either want the first n twin prime numbers or you want all the twin prime numbers less than n, but I don't see how you can ask for both. What is the output SUPPOSED to look like when they enter 7 and 10?

As far as the code highlighting, that's done automatically. If you want to override it, you can make something red or bold or whatever. I just make a semicolon bold, something no one would notice. I'm not crazy about the automatic code highlighting feature, but that's one way around it when you don't want it.

Thanks for your input. You Bool Function way appears to be the way to go, however, I lost the ability to get only Prime Numbers. I asked the question about the color fonts because my first post did not do so. Anyways, here's my code, I'll also show my output.

//============================================================================
// Name        : Homework_1.cpp
// Author      : Casey Smith
// Version     : Eclipse Galileo
// Copyright   : 9/5/2011
// Description : Homework 1 CompE 271 
//============================================================================

#include <iostream>
#include <math.h>

using namespace std;

bool isPrime(int number)
{

	int findingprime = number - 1;

		if (number%findingprime == 0) {

			number = number + 2;
			findingprime = number - 1;
		}
		else { //if there is a remainder
			findingprime = findingprime - 1;
		}
		while (findingprime == 1) {
			number = number + 2;
			findingprime = number - 1;
			}
return number;
}

int main(){

	int numberOfSets;
	int SetsFound = 0;
	int primeNumber = 3;
	cout << "How many twins do you want to find: ";
	cin >> numberOfSets;
	while (SetsFound < numberOfSets)
	{
	if (isPrime(primeNumber) && isPrime(primeNumber + 2))
	{
	cout << "(" << primeNumber << ", " << primeNumber + 2 << ")\n";
	primeNumber += 2;
	SetsFound++;
	}
	}

	return 0;
}

Here is my output.

How many twins do you want to find: 6
(3, 5)
(5, 7)
(7, 9)
(9, 11)
(11, 13)
(13, 15)

There was a bug in the code that I posted. you need to move line 46 in your code to be in between lines 48 and 49. Your isPrime function also needs some work. You have 9 in your prime pairs and 9 is not a prime number. Here is a isPrime function for you to digest

bool isPrime(int number)
{
    if (number == 1 || number == 2)
		return true;
    for (int i = 3; i * i <= number; i+=2)  // only check odd numbers and limit checks to sqrt(number) using * instead of sqrt()
    {
	if (number % i == 0)
		return false;
    }
    return true;
}

Bravo! Thank you.

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.