The last funtion IsPrimeNumber () is giving me an error that states ..."Local Function definitions are illegal"....can someone please tell me why? Thanks

``````/******************************************************************
Name :
Date :
Homework # :  Homework #6 Problem #1
Source  :
Action  :  Menu is displayed allows user to calculate
Miles per Gallon, Total minutes and seconds
and determine if number is a prime number or not

Notes  :
********************************************************************/
#include <iostream>
#include <stdlib.h>      // this header file requires the .h
using namespace std;

void FindMilesPerGallon(void);
void ConvertTotalSeconds(void);

void main()
{
int Choice;

system("cls");	// this clears the output screen

cin >> Choice;
while (Choice != 4)
{
switch (Choice)
{
case 1: FindMilesPerGallon();
break;
case 2: ConvertTotalSeconds();
break;
break;
default : cout << "Sorry illegal choice, Please try again\n\n";
}
cin >> Choice;

}
}

Action  :  This just displays the menu to the screen
Parameters  : none
returns     : nothing
***********************************************************************/
{
cout << "\n\nDo you want to:\n";
cout << "   1) Find miles per gallon\n";
cout << "   2) Convert total seconds to minutes and seconds\n";
cout << "   3) Determine if number is a prime number or not\n";
cout << "   4) QUIT\n\n";
cout << "   CHOICE --> ";
}

/********************  FindMilesPerGallon  ******************************
Action  : Ask user to input number of miles traveled and number of
gallons used then display the miles per gallon obtained.
Parameters : none
returns    : nothing
------------------------------------------------------------------------*/
void FindMilesPerGallon(void)
{
float Miles = 0, Gallons = 0, MPG;

cout << "Please enter the number of gallons used--> ";
cin >> Gallons;
cout << endl << endl;
cout << "Please enter miles traveled--> ";
cin >> Miles;
cout << endl << endl;

MPG = Miles / Gallons;

cout << "Your MPG is--> " << MPG << endl << endl;

}

/******************** ConvertTotalSeconds *********************
Action  : Ask user to input a given total number of seconds and then
displays the corresponding number of minutes and seconds
Parameters : none
returns    : nothing
-------------------------------------------------------------------------*/
void ConvertTotalSeconds (void)
{
int Seconds, Minutes, TotalSeconds = 0;

cout << "Please enter total seconds to convert--> ";
cin >> TotalSeconds;
cout << endl << endl;

Minutes = TotalSeconds / 60;
Seconds = TotalSeconds % 60;

cout << "There are " << Minutes << " minutes and " << Seconds << " seconds.";
cout << endl << endl;

Action  : Ask user to input positive integer and will determine if number
is a prime number or not.
Parameters  : none
Returns     : nothing
-------------------------------------------------------------------*/

{
int Num;

cout << "Please enter number to check if it's prime--> ";
cin >> Num;
cout << endl << endl;

if (Num % Num == 0)
{
cout << "Your number is prime.\n\n";
}
else
{
cout << "Your number is not prime.\n\n";
}
}``````
``````void IsPrimeNumber (void)
{
int Num, count;

cout << "Please enter number to check if it's prime--> ";
cin >> Num;
cout << endl << endl;

for (int x = 1; x <= Num; x++)
{
if (Num % x == 0)
{
count++;
}
}

if (count == 2)
{
cout << "Your number is prime." << endl;
}
else
{
cout << "Not a prime" << endl;
}

}``````

Hope that helps....

The last funtion IsPrimeNumber () is giving me an error that states ..."Local Function definitions are illegal"....can someone please tell me why?
This is because you didn't closed the function definition of void ConvertTotalSeconds (void) (i.e. you didn't put the closing } while ending the definition of void ConvertTotalSeconds (void)
)

Also, even if you did, your function isPrimeNumber() will not work. This is because in your function you used `if (Num % Num == 0)` and rather I should tell you that every number divided by itself leaves remainder zero.
So you function will always show each number is prime.
djextreme5 showed you how the definition of the isPrime should be, but there is a error in his program too.
He didn't initialized the variable count and incremented its value.
So the definition should be something like this :

``````[B]int[/B] IsPrimeNumber (int num)
{
for(int i = 2; i <= sqrt(num); i++)
if(num % i == 0)
{ cout<<"Is not a prime";
return 0;}

cout<<"Is a prime";
return1;
}``````

Moreover, the style
void function_name(void ) is not good. Use
void function_name() instead. The first one is old and outdated and is usually used by C programmers.

You don't have to go upto sqrt(num). The loop may be like this.

for(int i = 2; i < sqrt(num); i++)

>You don't have to go upto sqrt(num). The loop may be like this.
Wrong!!
consider 25: its square-root is 5, According to you, we should check till 4.
25 is not divisible by 2
25 is not divisible by 3
25 is not divisible by 4
So, is it a prime number?

Hence the correct thing is to check till the square-root including the square-root itself like I mentioned in my earlier post.

Yes you are right. My apologies...
Thanks for correcting me.

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.