User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,554 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,459 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 597 | Replies: 7 | Solved
Reply
Join Date: Oct 2007
Posts: 9
Reputation: redaqen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
redaqen redaqen is offline Offline
Newbie Poster

Using Returned Values and other questions...

  #1  
Oct 19th, 2007
Hey, again... I've got another assignment coming up and I'm having issues... again. Basically, my major stumbling block is figuring out how to set the int number = returned value of enterNewNumber.

Also, is there any simple way I can store the digits of an integer as seperate numbers in an array or string? ie int 2315 = int[4] = (2,3,1,5) etc etc

Thanks for your help... Now I'm crashing...

Code enclosed

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. #include<math.h>
  5.  
  6. int enterNewNumber();
  7. int printReversed(int);
  8. int checkPalindrome(int);
  9. int checkPrime(int);
  10. int printPrimeFast(int);
  11. int printPrimeSlow(int);
  12. int printPrimeFactors(int);
  13.  
  14. int main(void){
  15. int choice;
  16. int number;
  17. printf(" 1. Enter a new number.\n 2. Print number reversed.\n 3. Check if number is a palindrome.\n 4. Check if number is prime.\n 5. Print all prime numbers up to number (slow).\n 6. Print all prime numbers up to number (fast).\n 7. Print the prime factorization of the number.\n 8. Quit\n");
  18. printf("Please select an option from the list: ");
  19. scanf("%d", &choice);
  20. printf("You selected choice %d.\n", choice);
  21. switch(choice){
  22. case 1: number = enterNewNumber();
  23. break;
  24. case 2: printReversed(number);
  25. break;
  26. case 3: checkPalindrome(number);
  27. break;
  28. case 4: checkPrime(number);
  29. break;
  30. case 5: printPrimeFast(number);
  31. break;
  32. case 6: printPrimeSlow(number);
  33. break;
  34. case 7: printPrimeFactors(number);
  35. break;
  36. case 8: exit(0);
  37. break;
  38. default: printf("Your choice was invalid.\n Please choose a number from the list.\n");
  39. main();
  40.  
  41. }
  42. return 0;
  43. }
  44.  
  45. int enterNewNumber(){
  46. int n;
  47. printf("Please enter an int > 2:\n");
  48. scanf("%d", &n);
  49. if(n<2){
  50. printf("The int you entered was < 2.\n The int has been set equal to 2.\n");
  51. n=2;
  52. return n;
  53. main();
  54. }
  55. main();
  56. return n;
  57. }
  58.  
  59. int printReversed(int number) {
  60. int digit;
  61. int n = number;
  62. printf("This is your number: %d.\n", n);
  63. printf("This is your number reversed: ");
  64. while(n>0) {
  65. digit=n%10;
  66. printf("%d", digit);
  67. n=n/10;
  68. }
  69. printf("\n");
  70.  
  71. main();
  72. return 0;
  73. }
  74.  
  75. int checkPalindrome(int number){
  76. /*stuff*/
  77. return 0;
  78. }
  79.  
  80. int checkPrime(int number){
  81. int n=number, den;
  82. for(den=2; den*den<=number; ++den){
  83. if(number%den==0){
  84. printf("Your number is not prime.\n It is divisible by %d.\n", den);
  85. main();
  86. return 0;
  87. }
  88. printf("Your number is prime.\n");
  89.  
  90. return 0;
  91. }
  92.  
  93. int printPrimeFast(int number){
  94. /*stuff*/
  95. return 0;
  96. }
  97.  
  98. int printPrimeSlow(int number){
  99. /*stuff*/
  100. return 0;
  101. }
  102.  
  103. int printPrimeFactors(int number){
  104. /*stuff*/
  105. return 0;
  106. }
  107.  
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Using Returned Values and other questions...

  #2  
Oct 19th, 2007
Actually, you've got things just about right. The problem is that you are calling main() from the other functions. Don't do that.

In between lines 16 and 17 add:
while (TRUE) {

end the while statement between lines 41 and 42:
}

Then, get rid of every instance of main(); . That is, get rid of lines:
39, 52, 53, 55, 71, and 85.

You might also want to reconsider why so many of your functions return a value. For example, checkPrime() always returns zero, and the return value is ignored anyway (line 28), so just make it

void checkPrime( int );

and
void checkPrime( int number ) {
  int n=number, den;
  for(den=2; den*den<=number; ++den){
    if(number%den==0){
      printf("Your number is not prime.\n It is divisible by %d.\n", den);
      return;
    }
  printf("Your number is prime.\n");
  }

Hope this helps.
Last edited by Duoas : Oct 19th, 2007 at 5:42 am.
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: redaqen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
redaqen redaqen is offline Offline
Newbie Poster

Re: Using Returned Values and other questions...

  #3  
Oct 19th, 2007
When I inserted while(TRUE) it gave me an error saying that TRUE was undefined. Was that supposed to be while(true) ?

I went through the program and changed some of the functiions from ints to voids. For checkPrime, I now have it set to return 1 if it is not a prime and 0 if it is, so I kept it as an int.

Now, however, I get an error for line 102. It says error: expected declaration or statement at end of input.

Also, can anybody help me figure out an easy way to store the digits of an integer as an array or a string?

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. #include<math.h>
  5.  
  6. int enterNewNumber();
  7. void printReversed(int);
  8. void checkPalindrome(int);
  9. int checkPrime(int);
  10. void printPrimeFast(int);
  11. void printPrimeSlow(int);
  12. void printPrimeFactors(int);
  13.  
  14. int main(void){
  15. int choice;
  16. int number;
  17. while (true){
  18. printf(" 1. Enter a new number.\n 2. Print number reversed.\n 3. Check if number is a palindrome.\n 4. Check if number is prime.\n 5. Print all prime numbers up to number (slow).\n 6. Print all prime numbers up to number (fast).\n 7. Print the prime factorization of the number.\n 8. Quit\n");
  19. printf("Please select an option from the list: ");
  20. scanf("%d", &choice);
  21. printf("You selected choice %d.\n", choice);
  22. switch(choice){
  23. case 1: number = enterNewNumber();
  24. break;
  25. case 2: printReversed(number);
  26. break;
  27. case 3: checkPalindrome(number);
  28. break;
  29. case 4: checkPrime(number);
  30. break;
  31. case 5: printPrimeFast(number);
  32. break;
  33. case 6: printPrimeSlow(number);
  34. break;
  35. case 7: printPrimeFactors(number);
  36. break;
  37. case 8: exit(0);
  38. break;
  39. default: printf("Your choice was invalid.\n Please choose a number from the list.\n");
  40. main();
  41.  
  42. }
  43. }
  44. return 0;
  45. }
  46.  
  47. int enterNewNumber(){
  48. int n;
  49. printf("Please enter an int > 2:\n");
  50. scanf("%d", &n);
  51. if(n<2){
  52. printf("The int you entered was < 2.\n The int has been set equal to 2.\n");
  53. n=2;
  54. return n;
  55. }
  56. return n;
  57. }
  58.  
  59. void printReversed(int number) {
  60. int digit;
  61. int n = number;
  62. printf("This is your number: %d.\n", n);
  63. printf("This is your number reversed: ");
  64. while(n>0) {
  65. digit=n%10;
  66. printf("%d", digit);
  67. n=n/10;
  68. }
  69. printf("\n");
  70.  
  71. }
  72.  
  73. void checkPalindrome(int number){
  74. /*stuff*/
  75.  
  76. }
  77.  
  78. int checkPrime(int number){
  79. int n=number, den;
  80. for(den=2; den*den<=n; ++den){
  81. if(n%den==0){
  82. printf("Your number is not prime.\n It is divisible by %d.\n", den);
  83. return 1;
  84. }
  85. printf("Your number is prime.\n");
  86. return 0;
  87. }
  88.  
  89. void printPrimeFast(int number){
  90. /*stuff*/
  91.  
  92. }
  93.  
  94. void printPrimeSlow(int number){
  95. /*stuff*/
  96.  
  97. }
  98.  
  99. void printPrimeFactors(int number){
  100. /*stuff*/
  101.  
  102. }

In case the numbers don't line up, the error message
(Assignment4b.c:102: error: expected declaration or statement at end of input
Assignment4b.c:102: warning: control reaches end of non-void function)

Apply to the last function. printPrimeFactors.

Again, please and thank you. These issues have been causing me alot of frustration.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Using Returned Values and other questions...

  #4  
Oct 19th, 2007
you have mismatched braces in function checkPrime() that is causing the problem
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: redaqen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
redaqen redaqen is offline Offline
Newbie Poster

Re: Using Returned Values and other questions...

  #5  
Oct 19th, 2007
Oh geez... I feel really stupid now. Thanks a ton.
Reply With Quote  
Join Date: Oct 2007
Posts: 145
Reputation: hopalongcassidy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: Using Returned Values and other questions...

  #6  
Oct 20th, 2007
Originally Posted by redaqen View Post
When I inserted while(TRUE) it gave me an error saying that TRUE was undefined. Was that supposed to be while(true) ?

I went through the program and changed some of the functiions from ints to voids. For checkPrime, I now have it set to return 1 if it is not a prime and 0 if it is, so I kept it as an int.

Now, however, I get an error for line 102. It says error: expected declaration or statement at end of input.

Also, can anybody help me figure out an easy way to store the digits of an integer as an array or a string?

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. #include<math.h>
  5.  
  6. int enterNewNumber();
  7. void printReversed(int);
  8. void checkPalindrome(int);
  9. int checkPrime(int);
  10. void printPrimeFast(int);
  11. void printPrimeSlow(int);
  12. void printPrimeFactors(int);
  13.  
  14. int main(void){
  15. int choice;
  16. int number;
  17. while (true){
  18. printf(" 1. Enter a new number.\n 2. Print number reversed.\n 3. Check if number is a palindrome.\n 4. Check if number is prime.\n 5. Print all prime numbers up to number (slow).\n 6. Print all prime numbers up to number (fast).\n 7. Print the prime factorization of the number.\n 8. Quit\n");
  19. printf("Please select an option from the list: ");
  20. scanf("%d", &choice);
  21. printf("You selected choice %d.\n", choice);
  22. switch(choice){
  23. case 1: number = enterNewNumber();
  24. break;
  25. case 2: printReversed(number);
  26. break;
  27. case 3: checkPalindrome(number);
  28. break;
  29. case 4: checkPrime(number);
  30. break;
  31. case 5: printPrimeFast(number);
  32. break;
  33. case 6: printPrimeSlow(number);
  34. break;
  35. case 7: printPrimeFactors(number);
  36. break;
  37. case 8: exit(0);
  38. break;
  39. default: printf("Your choice was invalid.\n Please choose a number from the list.\n");
  40. main();
  41.  
  42. }
  43. }
  44. return 0;
  45. }
  46.  
  47. int enterNewNumber(){
  48. int n;
  49. printf("Please enter an int > 2:\n");
  50. scanf("%d", &n);
  51. if(n<2){
  52. printf("The int you entered was < 2.\n The int has been set equal to 2.\n");
  53. n=2;
  54. return n;
  55. }
  56. return n;
  57. }
  58.  
  59. void printReversed(int number) {
  60. int digit;
  61. int n = number;
  62. printf("This is your number: %d.\n", n);
  63. printf("This is your number reversed: ");
  64. while(n>0) {
  65. digit=n%10;
  66. printf("%d", digit);
  67. n=n/10;
  68. }
  69. printf("\n");
  70.  
  71. }
  72.  
  73. void checkPalindrome(int number){
  74. /*stuff*/
  75.  
  76. }
  77.  
  78. int checkPrime(int number){
  79. int n=number, den;
  80. for(den=2; den*den<=n; ++den){
  81. if(n%den==0){
  82. printf("Your number is not prime.\n It is divisible by %d.\n", den);
  83. return 1;
  84. }
  85. printf("Your number is prime.\n");
  86. return 0;
  87. }
  88.  
  89. void printPrimeFast(int number){
  90. /*stuff*/
  91.  
  92. }
  93.  
  94. void printPrimeSlow(int number){
  95. /*stuff*/
  96.  
  97. }
  98.  
  99. void printPrimeFactors(int number){
  100. /*stuff*/
  101.  
  102. }

In case the numbers don't line up, the error message
(Assignment4b.c:102: error: expected declaration or statement at end of input
Assignment4b.c:102: warning: control reaches end of non-void function)

Apply to the last function. printPrimeFactors.

Again, please and thank you. These issues have been causing me alot of frustration.


That's probably because checkprime has unbalanced braces ({}).
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 236
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 21
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Using Returned Values and other questions...

  #7  
Oct 20th, 2007
When I inserted while(TRUE) it gave me an error saying that TRUE was undefined. Was that supposed to be while(true) ?
TRUE is a commonly-defined constant -- windows.h defines it, along with a whole host of other libraries. true is a C++ keyword, and a C99 keyword if you include <stdbool.h>.

So in ordinary, ANSI C89-compatible code (which you probably want to write), it's easier to just use 1 instead of true or TRUE or whatever.
Incidentally, that creates what is called a forever loop, for obvious reasons. It never stops, until you use another flow control statement like break or return or exit(). (Not to be confused with an infinite loop, which is the same thing but which never exits -- that is, there are no return statements or anything. Infinite loops are usually unintentional.)

Anyway, another way to write a forever loop is like this:
I prefer this, because, in the immortal words of a programmer, "( ; ; ) is a code word for 'forever'."
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: redaqen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
redaqen redaqen is offline Offline
Newbie Poster

Re: Using Returned Values and other questions...

  #8  
Oct 20th, 2007
Thanks for all of your help! All of the major kinks have been worked out and I submitted the assignment sucessfully!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC