Hi this is my program. (Prime Numbers Application) Where I take limits of two numbers an upper and lower and define the primes with that scope of numbers.
for some reason, however, it seems a little off and i'm getting the following errors. can you explain a little of what my problem may be. Any help is greatly appreciated.
Error 1 - error C2601: 'testValue' : local function definitions are illegal
Error 2 - error C2440: 'return' : cannot convert from 'bool (__cdecl *)(int)' to 'double'
This is my code:
#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required to manipulate C++ stream I/O
#include <string> // required to access string functions
#include <cmath> // required to perform math operations.
#include <windows.h> //will be used to perform a series of beeps that creates a sound byte.
using namespace std;
// defines function prtotype testValue
double testValue (int);
void banner (void);
bool is_Prime(int); // is_prime function prototype? // boolean -- determines if true or false.
// end of function
int main(void)
{
int min = 0,
max = 0;
banner(); // Dispaly title Banner
double testValue (int n)
{
return is_Prime;
}
cout <<"\nEnter a lowerbound value: ";
cin >> min;
cout <<"\nEnter an upperbound value: ";
cin >> max;
while (min < 2 || max < 2 && min > max) // Range of numbers used > 2, max is not specified.
{
cout <<"\nPlease enter a number >= 2. Try again."<< endl << endl;
cout <<"Re-enter lowerbound value: ";
cin >> min;
cout <<"Re-enter upperbound value: ";
cin >> max;
}
return 0;
}
//This is the banner function
void banner (void)
{
system ("color 1F");
cout <<"\n**************************************************";
cout <<"\n* Welcome to the Prime Numbers Application *";
cout <<"\n**************************************************"<< endl << endl;
return;
}
// This is the function for is_prime.
double testValue(int n)
{
bool is_Prime = true;
for ( int i = 2; i <= n; i++)
{
for ( int n = 2; n < i; n++)
{
if ( i % n == 0 )
{
is_Prime = false;
break;
}
}
if (is_Prime)
{
cout <<"The Primes numbers are: "<< i << endl;
}
is_Prime = true;
}
Error 1 - error C2601: 'testValue' : local function definitions are illegal
Error 2 - error C2440: 'return' : cannot convert from 'bool (__cdecl *)(int)' to 'double'
Error 1: Move testValue outside of the main() function. You can put it ahead of the main or put the line: double testValue(int n); ahead of the main, and put the whole thing at the bottom of the file.
Error 2: Change testValue from : double testValue(int n) to bool testValue(int n)
#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required to manipulate C++ stream I/O
#include <string> // required to access string functions
#include <cmath> // required to perform math operations.
#include <windows.h> //will be used to perform a series of beeps that creates a sound byte.
usingnamespace std;
// defines function prtotype testValue
bool testValue (int);
void banner (void);
bool is_Prime(int); // is_prime function prototype? // boolean -- determines if true or false.
// end of function
int main(void)
{
int min = 0,
max = 0;
banner(); // Dispaly title Banner
cout <<"\nEnter a lowerbound value: ";
cin >> min;
cout <<"\nEnter an upperbound value: ";
cin >> max;
while (min < 2 || max < 2 && min > max) // Range of numbers used > 2, max is not specified.
{
cout <<"\nPlease enter a number >= 2. Try again."<< endl << endl;
cout <<"Re-enter lowerbound value: ";
cin >> min;
cout <<"Re-enter upperbound value: ";
cin >> max;
}
//Do something with min/max here and then call testValue(int n) to test a number.
return 0;
}
//This is the banner function
void banner (void)
{
system ("color 1F");
cout <<"\n**************************************************";
cout <<"\n* Welcome to the Prime Numbers Application *";
cout <<"\n**************************************************"<< endl << endl;
return;
}
// This is the function for is_prime.
bool testValue(int n)
{
bool is_Prime = true;
for ( int i = 2; i <= n; i++)
{
for ( int n = 2; n < i; n++)
{
if ( i % n == 0 )
{
is_Prime = false;
break;
}
}
if (is_Prime)
{
cout <<"The Primes numbers are: "<< i << endl;
}
is_Prime = true;
return is_Prime;
}