将 -1 返回给用户定义的函数会导致程序终止,退出代码为 0

Returning -1 to a user defined function is resulting to termination of program with exit code 0

在这个阶乘程序中,当输入任何非数字或负数时,程序应该要求租用该值,但在输出中程序正在终止。

是不是因为我在 display() 函数中 returning -1?如果是这样,那么如果函数意味着return一个值,那么是否必须return一个函数的变量(或其他函数)值?

#include <stdio.h>

int display();
void fact_fun(int num_fact);

int main() {
    int num = 0;
    char next;

    next = display();

    if (next == -1) { //WHEN ANY CHARACTER OR NEGATIVE NUMBER IS ENTERED IT WILL ASK TO RENTER
        printf("\nOnly positive number is allowed");
        display();
    }

    while (next >= 0) { //WHEN NEGATIVE NUMBER IS ENTERED IT WILL END THE LOOP
        num = next;
        fact_fun(num);
        next = display();
    }
    return 0;
}

int display() {
    char inp[10] = { 0 };
    int input;
    int index = 0;
    printf("\nEnter number to find factorial or press ENTER KEY to exit: ");

    while (((input = getchar()) != EOF) & (index < 10)) {
        if ((input >= '0') && (input <= '9')) {
            inp[index++] = input;
        } else
        if (input == '\n')
            break;
        else
            return -1;
    }
    input = atoi(inp);

    return input;
}

void fact_fun(int num_fact) {
    int fact = 1;
    if (num_fact == 0) {
        printf("\nFactorial of %d is 1", num_fact);
        return;
    } else {
        for (int i = 1; i <= num_fact; i++) {
            fact = fact * i;
        }
        printf("\nFactorial of %d is %d", num_fact, fact);
    }
}

此外,当我按回车键时,我得到如下输出:

Factorial of %d is 1
Enter number to find factorial or press ENTER KEY to exit:

当输入 \n 时程序终止。根据我的理解,它应该将 Enter 键和 \n 视为相同。如果不是那么有什么区别,我应该如何检查 ENTER KEY 值?

In this factorial program when entered any non numeric or negative number then the program should ask to renter the value

main() 函数中的 while 循环只会循环询问新数字,直到 input() return 为负数。你甚至记录了它:

while(next>=0) //WHEN NEGATIVE NUMBER IS ENTERED IT WILL END THE LOOP
...

当你在 input() 中执行 return -1 时,函数 returns -1 会将 next 设置为 -1 并结束循环。不久之后该程序就存在了。

从用户定义的函数中使用 return 语句到 return -1 本质上没有错。做这样的事情是很正常和普遍的。

默认情况下,用户输入是行缓冲的。一次从用户读取一行输入,解析它以断言输入有效性并仅计算有效输入的阶乘对于您的目的来说要简单得多。

另请注意,您可以简化计算,因为 0 的特殊情况与一般情况的代码是多余的。您还应该检查潜在的算术溢出,因为计算可能很容易超出类型 int 的范围并产生未定义的行为。

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int display();
void fact_fun(int num_fact);

int main() {
    int num;
    while ((num = display()) >= 0) {
        fact_fun(num);
    }
    return 0;
}

int display() {
    char buf[256];
    char *p;
    long value;

    for (;;) {
        printf("Enter number to find factorial or press ENTER KEY to exit: ");
        if (fgets(buf, sizeof buf, stdin) == NULL || *buf == '\n')
            return -1;
        errno = 0;
        value = strtol(buf, &p, 0);
        if (p == buf) {
            printf("Invalid input: not a number\n");
        } else {
        if (value < 0) {
            printf("Invalid input: negative values not allowed\n");
        } else
        if (errno != 0 || value > INT_MAX) {
            printf("Invalid input: value too large for type int\n");
        } else {
            return (int)value;
        }
    }
}

void fact_fun(int num_fact) {
    int fact = 1;
    for (int i = 1; i <= num_fact; i++) {
        if (fact > INT_MAX / i) {
            printf("Invalid input: arithmetic overflow\n");
            return;
        }
        fact = fact * i;
    }
    printf("Factorial of %d is %d\n", num_fact, fact);
}

这是您的代码并进行了一些更正。有两个问题。

首先,您必须完成读取输入,直到到达行尾或 EOF。

第二个是你需要两个错误码,一个是无效输入,一个是无输入。 (您在代码中的注释表明您想在无输入时退出)。

#include <stdio.h>
#include <stdlib.h>

int display();
void fact_fun(int num_fact);

int main() {
  //int num = 0;
    char next;

    while( 1 ) {

      next = display();
      if (next == -2) {
    break;
      }

      if (next == -1) {
    //WHEN ANY CHARACTER OR NEGATIVE NUMBER IS ENTERED IT WILL ASK TO RENTER
        printf("\nOnly positive number is allowed");
      }

      else {

        fact_fun(next);
      }
    }
    return 0;
}

int display() {
    char inp[10] = { 0 };
    char c;
    int input = 0;
    int index = 0;
    printf("\nEnter number to find factorial or press ENTER KEY to exit: ");

    while ( ((c = getchar()) != EOF) && (c != '\n') && (index < 10)) {
      if ( (c >= '0') && (c <= '9') ) {
    inp[index++] = c;
      } else {
    input = -1;
    break;
      }
    }

    // Finish inputting the line
    while ( (c != EOF) && (c != '\n') ) {
      c = getchar();
    }

    if ( !input ) {
      if ( index )
    input = atoi(inp);
      else
    input = -2;
    }

    return input;
}

void fact_fun(int num_fact) {
    int fact = 1;
    if (num_fact == 0) {
        printf("\nFactorial of %d is 1", num_fact);
        return;
    } else {
        for (int i = 1; i <= num_fact; i++) {
            fact = fact * i;
        }
        printf("\nFactorial of %d is %d", num_fact, fact);
    }
}