不确定为什么我的程序在我尝试关闭时会继续提示错误


#include <stdio.h>
#include <stdlib.h>
int add_even(int);
int add_odd(int);
int main() {
    int num, result_odd, result_even, even_count, odd_count;
    char name;
    printf("What is your name?n");
    scanf("%s", &name);
    while (num != 0) {
        printf("Enter a number:n");
        scanf("%d", &num);
        if (num % 2 == 1) {
            printf ("oddn");
            odd_count++;
        } else
        if (num == 0) {
            printf("%s, the numbers you have entered are broken down as follows:n",
                  name);
            result_even = add_even(num);
            printf("You entered %d even numbers with a total value of %dn",
                   even_count, result_even);
            result_odd = add_odd(num);
            printf("You entered %d odd numbers with a total value of %dn",
                   odd_count, result_odd);
        } else {
            printf("evenn");
            even_count++;
        }
    }
    return 0;
} 
int add_even(int num) {
    static int sum = 0;
    if (num % 2 != 0) {
        return 0;
    }
    sum += add_even(num);
    return sum;
}
int add_odd(int num) {
    static int sum = 0;
    if (num % 2 == 0) {
        return 0;
    }
    sum += add_odd(num);
    return sum;
}

谁能给我一些关于我做错了什么的见解?

代码的重点是从用户那里获取输入,直到他们决定停止。将evens与奇数分开。告诉他们他们放置了多少偶数/奇数和所有偶数/奇数的总数。

我了解如何将evens与赔率分开。我认为我的问题是我的功能。

您的代码中有多个问题:

  • scanf()试图将字符串存储到一个字符中时会导致不确定的行为。传递数组并指定最大长度。

  • 您应该检查scanf()的返回值:如果scanf()无法根据规范转换输入,则值未修改,因此未经初始化,并且随之而来。在您的情况下,如果在名称的提示下输入了2个或更多单词,则scanf("%d",...)由于未决数字输入而失败,因此未从Stdin和num读取其他字符。

  • num在第一个while (num != 0)中未经启发,导致不确定的行为。

  • 函数add_even()add_odd()仅用于num == 0,切勿概括任何内容。

  • 函数add_even()add_odd()应始终返回总和,并添加参数num的值是正确的奇偶校验。他们目前通过无限期地将自己递归地称为不确定的行为。
  • odd_counteven_count是不可分化的,因此计数将不确定并阅读其调用行为。

尽管上面提到的所有未定义行为的来源,但如果您可能要为该名称输入多个单词,则程序会不断提示而不会期望答案。%s仅转换一个单词,其余的作为数字输入,该输入在循环中反复失败。由于您不验证scanf()的返回值。

,这些失败没有引起注意。

这是一个更正的版本:

#include <stdio.h>
#include <stdlib.h>
int add_even(int);
int add_odd(int);
int main(void) {
    int num, result_odd, result_even, even_count = 0, odd_count = 0;
    char name[100];
    printf("What is your name? ");
    if (scanf("%99[^n]", name) != 1)
        return 1;
    for (;;) {
        printf("Enter a number: ");
        if (scanf("%d", &num) != 1 || num == 0)
            break;
        if (num % 2 == 1) {
            printf("oddn");
            odd_count++;
            add_odd(num);
        } else {
            printf("evenn");
            even_count++;
            add_even(num);
        }
        printf("%s, the numbers you have entered are broken down as follows:n", name);
        result_even = add_even(0);
        printf("You entered %d even numbers with a total value of %dn",
               even_count, result_even);
        result_odd = add_odd(0);
        printf("You entered %d odd numbers with a total value of %dn",
               odd_count, result_odd);
    }
    return 0;
} 
int add_even(int num) {
    static int sum = 0;
    if (num % 2 == 0) {
        sum += num;
    }
    return sum;
}
int add_odd(int num) {
    static int sum = 0;
    if (num % 2 != 0) {
        sum += num;
    }
    return sum;
}

您已声明:

char name;                       // One single letter, such as 'A', or 'M'
printf("What is your name?n");  // Please enter a whole bunch of letters!
scanf("%s", &name);              // Not enough space to store the response!

您真正想要的更像

char name[31];                   // Up to 30 letters, and an End-of-String marker
printf("What is your name?n"); // Please enter a whole bunch of letters!
scanf("%s", name);              // name is the location to put all those letters
                                // (but not more than 30!)

最新更新