函数从不同的函数中提取变量值,但我不希望它也不知道它是如何做的

时间:2014-09-28 02:31:39

标签: c function variables scope int

对于我的编程类,我们正在创建一个调用多个其他函数的main()函数。其中两个函数是numberStats()和triad(),它们都有自己的变量,没有使用全局变量,并且在triad()之前立即调用numberStats()。两个代码都没有类似的变量名,但出于某种原因,当我使用三元组函数时,它要求用户输入3个整数,它会正确地吐出第一个int,但第二个和第三个整数是输入到numberStats()的两个整数功能。任何人都可以解释为什么会发生这种情况以及我可以做些什么来解决它?提前谢谢你,这里有新鲜血液。    编辑* 这里是输出代码:(如果它是斜体,那么它是我输入的数字作为输入)

    numberStats: Please enter two positive ints: 5 800  //I input 5 and 800

    800 is larger
    5 is smaller
    800 + 5 = 805
    800 - 5 = 795
    800 * 5 = 4000
    800 % 5 = 0

    triad: Please enter three positive ints:
    1 2 3    //I input 1 2 and 3
    you entered 1, 5, and 800
    Not all equal
    Sorted order: 1, 5, 800

我显然没有为三合一功能输入1,5和800,为什么它会告诉我我做了?

编辑** numberStats()和triad()的源代码:

    void numberStats(void)
{
    int var1, var2, a, b, c, d;

    printf("numberStats: Please enter two positive ints: ");
    scanf("%d %d",&var1, &var2);

    if (var1 > var2){
            printf("%d is larger\n",var1);
            printf("%d is smaller\n",var2);

    a = var1 + var2;
    b = var1 - var2;
    c = var1 * var2;
    d = var1 % var2;

            printf("%d + %d = %d\n",var1,var2,a);
            printf("%d - %d = %d\n",var1,var2,b);
            printf("%d * %d = %d\n",var1,var2,c);
            printf("%d %% %d = %d\n",var1,var2,d);
    }
    else if (var1 < var2){
            printf("\n%d is larger\n",var2);
            printf("%d is smaller\n",var1);

    a = var2 + var1;
    b = var2 - var1;
    c = var2 * var1;
    d = var2 % var1;

            printf("%d + %d = %d\n",var2,var1,a);
            printf("%d - %d = %d\n",var2,var1,b);
            printf("%d * %d = %d\n",var2,var1,c);
            printf("%d %% %d = %d\n",var2,var1,d);
    }
    else if (var1 == var2){
            printf("%d and %d are the same\n",var1,var2);

    a = var1 + var2;
    b = var1 - var2;
    c = var1 * var2;
    d = var1 % var2;

            printf("%d + %d = %d\n",var1,var2,a);
            printf("%d - %d = %d\n",var1,var2,b);
            printf("%d * %d = %d\n",var1,var2,c);
            printf("%d %% %d = %d\n",var1,var2,d);
    }
}

void triad(void)
{
    int x, y, z, low, mid, high;

    printf("\ntriad: Please enter three positive ints: \n");
    scanf("%d, %d, %d", &x, &y, &z);
    printf("you entered %d, %d, and %d\n", x, y, z);

    if (x == y && y == z){
            printf("All equal\n");
    }
    if (x <= y && x <= z){
            low = x;
            if (y <= z){
                    mid = y;
                    high = z;
            }
            else{
                    mid = z;
                    high = y;
            }
            printf("Not all equal\n");
    }
    if (x >= y && x >= z){
            high = x;
            if (y <= z){
                    low = y;
                    mid = z;
            }
            else{
                    low = z;
                    mid = y;
            }
            printf("Not all equal\n");
    }
    if ((x >= y && x <= z) || (x <= y && x >= z)){
            mid = x;
            if (y >= z){
                    high = y;
                    low = z;
            }
            else{
                    high = z;
                    low = y;
            }
            printf("Not all equal\n");
    }
    printf("Sorted order: %d, %d, %d\n", low, mid, high);
}

1 个答案:

答案 0 :(得分:1)

功能triad()scanf()造成了麻烦:

int x, y, z, low, mid, high;

printf("\ntriad: Please enter three positive ints: \n");
scanf("%d, %d, %d", &x, &y, &z);

您在数据中寻找逗号,但没有输入它们(您提供了“1 2 3”,但您需要使用逗号(和可选空格)分隔数字“1,2,3” 。 scanf()返回1而不是3,但您忽略了它。别!每次检查您的输入。

因此,您在yz中拥有准随机值,这就是您遇到问题的原因。

基本的调试技术是打印刚刚读取的值:

if (scanf("%d, %d, %d", &x, &y, &z) != 3)
   …report error and bail out…
printf("x = %d, y = %d, z = %d\n", x, y, z);