•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> #include<stdbool.h> #include<math.h> int enterNewNumber(); int printReversed(int); int checkPalindrome(int); int checkPrime(int); int printPrimeFast(int); int printPrimeSlow(int); int printPrimeFactors(int); int main(void){ int choice; int number; 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"); printf("Please select an option from the list: "); scanf("%d", &choice); printf("You selected choice %d.\n", choice); switch(choice){ case 1: number = enterNewNumber(); break; case 2: printReversed(number); break; case 3: checkPalindrome(number); break; case 4: checkPrime(number); break; case 5: printPrimeFast(number); break; case 6: printPrimeSlow(number); break; case 7: printPrimeFactors(number); break; case 8: exit(0); break; default: printf("Your choice was invalid.\n Please choose a number from the list.\n"); main(); } return 0; } int enterNewNumber(){ int n; printf("Please enter an int > 2:\n"); scanf("%d", &n); if(n<2){ printf("The int you entered was < 2.\n The int has been set equal to 2.\n"); n=2; return n; main(); } main(); return n; } int printReversed(int number) { int digit; int n = number; printf("This is your number: %d.\n", n); printf("This is your number reversed: "); while(n>0) { digit=n%10; printf("%d", digit); n=n/10; } printf("\n"); main(); return 0; } int checkPalindrome(int number){ /*stuff*/ return 0; } int 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); main(); return 0; } printf("Your number is prime.\n"); return 0; } int printPrimeFast(int number){ /*stuff*/ return 0; } int printPrimeSlow(int number){ /*stuff*/ return 0; } int printPrimeFactors(int number){ /*stuff*/ return 0; }
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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:
end the while statement between lines 41 and 42:
Then, get rid of every instance of
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
and
Hope this helps.
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.
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
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?
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.
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?
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> #include<stdbool.h> #include<math.h> int enterNewNumber(); void printReversed(int); void checkPalindrome(int); int checkPrime(int); void printPrimeFast(int); void printPrimeSlow(int); void printPrimeFactors(int); int main(void){ int choice; int number; while (true){ 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"); printf("Please select an option from the list: "); scanf("%d", &choice); printf("You selected choice %d.\n", choice); switch(choice){ case 1: number = enterNewNumber(); break; case 2: printReversed(number); break; case 3: checkPalindrome(number); break; case 4: checkPrime(number); break; case 5: printPrimeFast(number); break; case 6: printPrimeSlow(number); break; case 7: printPrimeFactors(number); break; case 8: exit(0); break; default: printf("Your choice was invalid.\n Please choose a number from the list.\n"); main(); } } return 0; } int enterNewNumber(){ int n; printf("Please enter an int > 2:\n"); scanf("%d", &n); if(n<2){ printf("The int you entered was < 2.\n The int has been set equal to 2.\n"); n=2; return n; } return n; } void printReversed(int number) { int digit; int n = number; printf("This is your number: %d.\n", n); printf("This is your number reversed: "); while(n>0) { digit=n%10; printf("%d", digit); n=n/10; } printf("\n"); } void checkPalindrome(int number){ /*stuff*/ } int checkPrime(int number){ int n=number, den; for(den=2; den*den<=n; ++den){ if(n%den==0){ printf("Your number is not prime.\n It is divisible by %d.\n", den); return 1; } printf("Your number is prime.\n"); return 0; } void printPrimeFast(int number){ /*stuff*/ } void printPrimeSlow(int number){ /*stuff*/ } void printPrimeFactors(int number){ /*stuff*/ }
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
you have mismatched braces in function checkPrime() that is causing the problem
•
•
•
•
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?
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> #include<stdbool.h> #include<math.h> int enterNewNumber(); void printReversed(int); void checkPalindrome(int); int checkPrime(int); void printPrimeFast(int); void printPrimeSlow(int); void printPrimeFactors(int); int main(void){ int choice; int number; while (true){ 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"); printf("Please select an option from the list: "); scanf("%d", &choice); printf("You selected choice %d.\n", choice); switch(choice){ case 1: number = enterNewNumber(); break; case 2: printReversed(number); break; case 3: checkPalindrome(number); break; case 4: checkPrime(number); break; case 5: printPrimeFast(number); break; case 6: printPrimeSlow(number); break; case 7: printPrimeFactors(number); break; case 8: exit(0); break; default: printf("Your choice was invalid.\n Please choose a number from the list.\n"); main(); } } return 0; } int enterNewNumber(){ int n; printf("Please enter an int > 2:\n"); scanf("%d", &n); if(n<2){ printf("The int you entered was < 2.\n The int has been set equal to 2.\n"); n=2; return n; } return n; } void printReversed(int number) { int digit; int n = number; printf("This is your number: %d.\n", n); printf("This is your number reversed: "); while(n>0) { digit=n%10; printf("%d", digit); n=n/10; } printf("\n"); } void checkPalindrome(int number){ /*stuff*/ } int checkPrime(int number){ int n=number, den; for(den=2; den*den<=n; ++den){ if(n%den==0){ printf("Your number is not prime.\n It is divisible by %d.\n", den); return 1; } printf("Your number is prime.\n"); return 0; } void printPrimeFast(int number){ /*stuff*/ } void printPrimeSlow(int number){ /*stuff*/ } void printPrimeFactors(int number){ /*stuff*/ }
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 ({}).
•
•
•
•
When I inserted while(TRUE) it gave me an error saying that TRUE was undefined. Was that supposed to be while(true) ?
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.
while(1)
Anyway, another way to write a forever loop is like this:
for(;;)
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
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- c++ functions to compare numbers (C++)
- Returning local array (C++)
- Selecting MYSQL into fields (HTML and CSS)
- How to call a perl script in the C# app? (C#)
- Question about string pattern matching (Java)
- while() not looping... (PHP)
- Need real time in XP (Visual Basic 4 / 5 / 6)
- Weird segfault involving popen (C)
- help with questions (Java)
Other Threads in the C Forum
- Previous Thread: Sign/magnitude mantissa
- Next Thread: reading in from vector to a two dimensional array



Linear Mode