954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using Returned Values and other questions...

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

#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;
}
redaqen
Newbie Poster
9 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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?

#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.

redaqen
Newbie Poster
9 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

you have mismatched braces in function checkPrime() that is causing the problem

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Oh geez... I feel really stupid now. Thanks a ton.

redaqen
Newbie Poster
9 posts since Oct 2007
Reputation Points: 10
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?

#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 ({}).

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 
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 .

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)

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:

for(;;)

I prefer this, because, in the immortal words of a programmer, "( ; ; ) is a code word for 'forever'." ;)

dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 

Thanks for all of your help! All of the major kinks have been worked out and I submitted the assignment sucessfully!

redaqen
Newbie Poster
9 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You