| | |
My last function is giving me an error and not sure why? HELP...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
The last funtion IsPrimeNumber () is giving me an error that states ..."Local Function definitions are illegal"....can someone please tell me why? Thanks
C++ Syntax (Toggle Plain Text)
/****************************************************************** 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 DisplayMenu(void); void FindMilesPerGallon(void); void ConvertTotalSeconds(void); void IsPrimeNumber(void); void main() { int Choice; system("cls"); // this clears the output screen DisplayMenu(); cin >> Choice; while (Choice != 4) { switch (Choice) { case 1: FindMilesPerGallon(); break; case 2: ConvertTotalSeconds(); break; case 3: IsPrimeNumber(); break; default : cout << "Sorry illegal choice, Please try again\n\n"; } DisplayMenu(); cin >> Choice; } } /********************* DisplayMenu ************************************ Action : This just displays the menu to the screen Parameters : none returns : nothing ***********************************************************************/ void DisplayMenu() { 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; /*********************** IsPrimeNumber()**************************** Action : Ask user to input positive integer and will determine if number is a prime number or not. Parameters : none Returns : nothing -------------------------------------------------------------------*/ void IsPrimeNumber (void) { 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"; } }
Last edited by tpetsovi; Mar 3rd, 2009 at 9:13 pm.
c++ Syntax (Toggle Plain Text)
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....
Last edited by djextreme5; Mar 3rd, 2009 at 9:16 pm.
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
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 :
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.
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 :
int 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.
Last edited by siddhant3s; Mar 3rd, 2009 at 11:34 pm.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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.
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.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
![]() |
Similar Threads
- What should be my arguments for function called in main() (C++)
- Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource (PHP)
- Calling Shell Commands (Python)
- Fatal error: require once (PHP)
- Easy question with function (C)
- Error handling (PHP)
- session variable error (PHP)
- error: non-lvalue in unary `&' (C)
- Passing Arrays to function in Visual C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: std::string and exceptions in constructor
- Next Thread: displaying system time every few minutes
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





