简体   繁体   English

如何在C中终止程序?

[英]how to terminate the program in c?

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! 我尝试了return,但是没有找到合适的位置保存它,因此我完成了代码,并提供了所需的输出,但是它没有终止! 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");
    }        
}

It's in endless loop 无休止的循环

main calls get_input which calls is_valid which calls get_input making a indirect recursion there. main调用get_input ,后者调用is_valid ,后者调用get_input进行间接递归。

If you want to terminate, enter some if condition for num like if (num < 0) return 如果要终止,请为num输入一些if条件,例如if (num < 0) return

When you use if-else statement, you should use {} to create a block, unless the block is single lined. 使用if-else语句时,除非该块是单行的,否则应使用{}创建一个块。

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. 每次调用is_valid()时,都会导致调用get_input(),从而导致无限循环。 to prevent the loop, insert the call to get_input() only when needed. 为了防止循环,仅在需要时才插入对get_input()的调用。

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-else语句,最后的else-if是多余的。 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. 如果num既不大于9,也不等于或小于1,则它必须是合法的输入,并且无需在其中添加if语句。

main() should end with return 0 if your program succeeded. 如果您的程序成功, main()应该以return 0结尾。 Here's a short explanation of some status codes you can return: https://stackoverflow.com/a/22604382/1137699 这是您可以返回的一些状态代码的简短说明: https : //stackoverflow.com/a/22604382/1137699

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM