0

so i'm done with my code and it gives the required output, but it doesn't terminate! how can i fix it? i tried return but didn't find the right place to keep it so i'm done with my code and it gives the required output, but it doesn't terminate! how can i fix it? i tried return but didn't find the right place to keep it

#include<stdio.h>

void get_input();   
void is_valid(int num);  
void print_pattern(int num);  

void main ()
{
    get_input();    
}

void get_input()
{
    int a;
    printf("Enter an odd number less than or equal to 9 and greater than 0 > "); 
    scanf("%d",&a);
    is_valid(a);
}

void is_valid(int num)
{
    if(num > 9)
        printf("You have entered a number greater than 9. Please try again.\n\n"); 
    else if (num < 1)
        printf("You have entered a number less than 1. Please try again.\n\n"); 
    else if (num % 2 == 0)
        printf("You have entered an even number. Please try again\n\n"); 
    else if (num >= 1 && num <= 9 && num%2!=0)    
        print_pattern(num);
    get_input();              
}

void print_pattern(int num)
{
    int j,k,l;
    for ( j = 0; j<num/2 ; j++ ){
        for ( k = 0; k<num/2 - j; k++){
            printf("  ");
        }
        for( l=0; l < (2*j + 1); l++){
            printf("%2d",l+1);
        }
        printf("\n");    
    }    

    for( l=0; l <num; l++) // to print middle
    {
        printf("%2d",l+1);
    }
    printf("\n");

    // to print bottom
    for ( j = 0; j<num/2 ; j++){
        for ( k = 0; k<j+1 ; k++){
            printf("  ");
        }
        for ( l=1; l< num - (2*j + 1); l++){
            printf("%2d",l);
        }
        printf("\n");
    }        
}
DYZ
  • 55,249
  • 10
  • 64
  • 93
Max
  • 29
  • 4
  • What does it do instead of terminating? Where is it stuck? – Carcigenicate Feb 20 '17 at 17:43
  • 4
    You have mutual recursion between `get_input` and `is_valid`. You need to allow one of them to return without calling the other. What do you want to decide when to exit? – Carcigenicate Feb 20 '17 at 17:44
  • 2
    Start indenting your code first. – Jabberwocky Feb 20 '17 at 17:45
  • `print_pattern(num);` --> `{ print_pattern(num); return; }` – BLUEPIXY Feb 20 '17 at 17:47
  • 1
    You have a design issue. It looks that you should have a loop in get_input() like `do { /*print blablabla*/ } while(!is_valid())` and your is_valid() shouldn't recursively calls get_input(), instead, it should return 0 for false, and 1 for true. – Luciano Feb 20 '17 at 17:55
  • `get_input` and `is_valid` are calling themselves mutually, the design of your program is broken. – Jabberwocky Feb 20 '17 at 17:58

3 Answers3

1

It's in endless loop

main calls get_input which calls is_valid which calls get_input making a indirect recursion there.

If you want to terminate, enter some if condition for num like if (num < 0) return

Martin Macak
  • 3,507
  • 2
  • 30
  • 54
1

When you use if-else statement, you should use {} to create a block, unless the block is single lined.

For example, when you use

if(num > 9)
printf("You have entered a number greater than 9. Please try again.\n\n"); 
else if (num < 1)
printf("You have entered a number less than 1. Please try again.\n\n"); 
else if (num % 2 == 0)
printf("You have entered an even number. Please try again\n\n"); 
else if (num >= 1 && num <= 9 && num%2!=0)
print_pattern(num);
get_input();          

it will be read as

if(num > 9)
{
    printf("You have entered a number greater than 9. Please try again.\n\n"); 
}
else if (num < 1)
{
    printf("You have entered a number less than 1. Please try again.\n\n"); 
}
else if (num % 2 == 0)
{
    printf("You have entered an even number. Please try again\n\n"); 
}
else if (num >= 1 && num <= 9 && num%2!=0)
{
    print_pattern(num);
}
get_input(); 

This will result in calling get_input() each time you call is_valid(), ending in an endless loop. to prevent the loop, insert the call to get_input() only when needed.

Furthermore, make sure that your blocks (the parts between each { }) are well defined, and you are not closing them prematurely or opening blocks without closing them. This can also result in looping, especially if done wrong where you have loops.

Also, regarding the if-else statements, the last else-if is redundant. If num is neither greater than 9, nor even, nor smaller than 1, then it must be a legal input, and there is no need to add the if statement there.

Dan Sheinin
  • 59
  • 10
  • Even if conditionally calling `get_input()` rather than calling it every time will allow the program to terminate, the recursion depth will still increase every time that condition is met... he'd be better of using a loop instead of recursion. – Dmitri Feb 20 '17 at 18:00
-3

main() should end with return 0 if your program succeeded. Here's a short explanation of some status codes you can return: https://stackoverflow.com/a/22604382/1137699

Community
  • 1
  • 1
theopak
  • 62
  • 4
  • 3
    The main program terminates whether it has an explicit `return` or not - unless it has a case of an infinite loop or infinite recursion. – DYZ Feb 20 '17 at 17:48
  • 2
    Actually `main()` implicitly returns 0 if there's no `return` (if the end of it is ever reached) – Dmitri Feb 20 '17 at 17:48
  • 1
    @Dmitri This is true only in C99/C11. In other standards, the return value is undefined. – DYZ Feb 20 '17 at 17:50
  • 3
    His problem is that the end of main is never even reached, so this won't do anything. – Carcigenicate Feb 20 '17 at 17:57
  • 2
    The problem in this program is that `get_input()` never returns. It doesn't matter what you put after it. – Barmar Feb 20 '17 at 18:19