Hi - I am brand new to C++ and am just learning about functions and passing arguments between functions.
Below is my program. In the function int Prime (int n) I am trying to somehow take the code from bool is prime(int num) to determine the nth location of a prime number. Any help is greatly appreciated!
#include <iostream>
usingnamespace std;
bool IsPrime(int); //function declaration before it is called, specifies type
int Prime(int); //function declaration before it is called
void main()
{
char response;
int n, num, count=0;
do {
cout << "Enter F to find the Nth prime number: " << endl;
cout << "Enter T to test the value to see if it is prime: " << endl;
cout << "Enter Q to terminate the program: " << endl;
cin >> response;
if (response == 't'|| response == 'T'){
cout << "Enter A Value Greater Than Zero: ";
cin >> num;
if (num > 0) {
if (IsPrime(num) == false) {
cout << "Your Number Is Not Prime";
}
else {
if (IsPrime(num) == true) {
cout << "Your Number Is Prime";
}
}
}
else
cout << "Enter A Value Greater Than Zero: ";
cin >> num;
}
else {
if (response == 'F' || response =='f') {
cout << "Enter a Value Greater Than Zero: ";
cin >> n;
if (n > 0) {
cout << "The nth Prime Number where n is " << n << " is " << Prime(n)<< endl;
}
else cout << "Your Number Is Invalid."; //get rid of prompt here?
}
}
}
while (response != 'Q'); //used code while (response !='Q' || response !='q')
//and will not work for either Q or q when or is entered
}
// for 'T' Value
bool IsPrime(int num) // function declarator
{
for (int j = 2; j <= num / 2; j++)
if (num % j == 0)
returnfalse; //not prime
returntrue; //is prime
}
// for 'F' Value
int Prime(int n) // function declarator
{
int count = 0;
while (IsPrime(n) == false){
return count++;
}
}