My last function is giving me an error and not sure why? HELP...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 5
Reputation: tpetsovi is an unknown quantity at this point 
Solved Threads: 0
tpetsovi tpetsovi is offline Offline
Newbie Poster

My last function is giving me an error and not sure why? HELP...

 
0
  #1
Mar 3rd, 2009
The last funtion IsPrimeNumber () is giving me an error that states ..."Local Function definitions are illegal"....can someone please tell me why? Thanks





  1. /******************************************************************
  2. Name :
  3. Date :
  4. Homework # : Homework #6 Problem #1
  5. Source :
  6. Action : Menu is displayed allows user to calculate
  7. Miles per Gallon, Total minutes and seconds
  8. and determine if number is a prime number or not
  9.  
  10. Notes :
  11. ********************************************************************/
  12. #include <iostream>
  13. #include <stdlib.h> // this header file requires the .h
  14. using namespace std;
  15.  
  16. void DisplayMenu(void);
  17. void FindMilesPerGallon(void);
  18. void ConvertTotalSeconds(void);
  19. void IsPrimeNumber(void);
  20.  
  21.  
  22. void main()
  23. {
  24. int Choice;
  25.  
  26. system("cls"); // this clears the output screen
  27.  
  28. DisplayMenu();
  29. cin >> Choice;
  30. while (Choice != 4)
  31. {
  32. switch (Choice)
  33. {
  34. case 1: FindMilesPerGallon();
  35. break;
  36. case 2: ConvertTotalSeconds();
  37. break;
  38. case 3: IsPrimeNumber();
  39. break;
  40. default : cout << "Sorry illegal choice, Please try again\n\n";
  41. }
  42. DisplayMenu();
  43. cin >> Choice;
  44.  
  45. }
  46. }
  47.  
  48. /********************* DisplayMenu ************************************
  49. Action : This just displays the menu to the screen
  50. Parameters : none
  51. returns : nothing
  52. ***********************************************************************/
  53. void DisplayMenu()
  54. {
  55. cout << "\n\nDo you want to:\n";
  56. cout << " 1) Find miles per gallon\n";
  57. cout << " 2) Convert total seconds to minutes and seconds\n";
  58. cout << " 3) Determine if number is a prime number or not\n";
  59. cout << " 4) QUIT\n\n";
  60. cout << " CHOICE --> ";
  61. }
  62.  
  63. /******************** FindMilesPerGallon ******************************
  64. Action : Ask user to input number of miles traveled and number of
  65.   gallons used then display the miles per gallon obtained.
  66. Parameters : none
  67. returns : nothing
  68. ------------------------------------------------------------------------*/
  69. void FindMilesPerGallon(void)
  70. {
  71. float Miles = 0, Gallons = 0, MPG;
  72.  
  73. cout << "Please enter the number of gallons used--> ";
  74. cin >> Gallons;
  75. cout << endl << endl;
  76. cout << "Please enter miles traveled--> ";
  77. cin >> Miles;
  78. cout << endl << endl;
  79.  
  80. MPG = Miles / Gallons;
  81.  
  82. cout << "Your MPG is--> " << MPG << endl << endl;
  83.  
  84. }
  85.  
  86. /******************** ConvertTotalSeconds *********************
  87. Action : Ask user to input a given total number of seconds and then
  88.   displays the corresponding number of minutes and seconds
  89. Parameters : none
  90. returns : nothing
  91. -------------------------------------------------------------------------*/
  92. void ConvertTotalSeconds (void)
  93. {
  94. int Seconds, Minutes, TotalSeconds = 0;
  95.  
  96. cout << "Please enter total seconds to convert--> ";
  97. cin >> TotalSeconds;
  98. cout << endl << endl;
  99.  
  100. Minutes = TotalSeconds / 60;
  101. Seconds = TotalSeconds % 60;
  102.  
  103. cout << "There are " << Minutes << " minutes and " << Seconds << " seconds.";
  104. cout << endl << endl;
  105.  
  106. /*********************** IsPrimeNumber()****************************
  107. Action : Ask user to input positive integer and will determine if number
  108.   is a prime number or not.
  109. Parameters : none
  110. Returns : nothing
  111. -------------------------------------------------------------------*/
  112.  
  113. void IsPrimeNumber (void)
  114. {
  115. int Num;
  116.  
  117. cout << "Please enter number to check if it's prime--> ";
  118. cin >> Num;
  119. cout << endl << endl;
  120.  
  121. if (Num % Num == 0)
  122. {
  123. cout << "Your number is prime.\n\n";
  124. }
  125. else
  126. {
  127. cout << "Your number is not prime.\n\n";
  128. }
  129. }
Last edited by tpetsovi; Mar 3rd, 2009 at 9:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: djextreme5 is on a distinguished road 
Solved Threads: 4
djextreme5's Avatar
djextreme5 djextreme5 is offline Offline
Junior Poster in Training

Re: My last function is giving me an error and not sure why? HELP...

 
0
  #2
Mar 3rd, 2009
  1. void IsPrimeNumber (void)
  2. {
  3. int Num, count;
  4.  
  5. cout << "Please enter number to check if it's prime--> ";
  6. cin >> Num;
  7. cout << endl << endl;
  8.  
  9. for (int x = 1; x <= Num; x++)
  10. {
  11. if (Num % x == 0)
  12. {
  13. count++;
  14. }
  15. }
  16.  
  17. if (count == 2)
  18. {
  19. cout << "Your number is prime." << endl;
  20. }
  21. else
  22. {
  23. cout << "Not a prime" << endl;
  24. }
  25.  
  26.  
  27. }


Hope that helps....
Last edited by djextreme5; Mar 3rd, 2009 at 9:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: My last function is giving me an error and not sure why? HELP...

 
0
  #3
Mar 3rd, 2009
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 :

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: My last function is giving me an error and not sure why? HELP...

 
0
  #4
Mar 4th, 2009
You don't have to go upto sqrt(num). The loop may be like this.

for(int i = 2; i < sqrt(num); i++)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: My last function is giving me an error and not sure why? HELP...

 
0
  #5
Mar 4th, 2009
>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.
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: My last function is giving me an error and not sure why? HELP...

 
0
  #6
Mar 4th, 2009
Yes you are right. My apologies...
Thanks for correcting me.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC