【问题标题】:My second function stub seems to infinite loop?我的第二个函数存根似乎无限循环?
【发布时间】:2013-11-14 15:13:55
【问题描述】:

我有两个相对相同的存根函数,但是当我调用第二个存根时,它似乎是无限循环,我不知道为什么。我在这段代码中调用的无限循环函数是 convert_weight();。我怀疑这是否是主程序问题,因为它被调用并毫无问题地打印出前几个 printfs,但是当使用来自 scanf 的用户输入时,它会进入无限循环。任何帮助都会很有用,谢谢。

#include <stdio.h>
void convert_lengths(int users_choice);
void convert_weight(int users_choice);
void length_to_metric(void);
void length_to_us(void);
void weight_to_metric(void);
void weight_to_us(void);

int main()
{
    int users_choice;
    do
    {
        printf("Do you want to convert length or weights?\n");
        printf("1 for length\n");
        printf("2 for weights\n");
        printf("0 to end program\n");
        scanf("%d", &users_choice);
        if(users_choice == 1)
        {
            convert_lengths(users_choice);
        }
        if(users_choice == 2)
        {
            convert_weight(users_choice);
        }
    }while(users_choice != 0);
    return 0;
}
void convert_lengths(int a)
{
    int b;
    do
    {
        if(a == 1)
        {
            printf("You have chosen to convert lengths\n");
            printf("What units do you want to convert?\n");
            printf("- 1 to convert feet/inches to meters/centimeters\n");
            printf("- 2 to convert from meters/centimeters to feet/inches\n");
            printf("- 0 to go back to other options\n");
            scanf("%d", &b);
            if(b == 1)
            {
                printf("You have chosen to convert feet/inches to meters/centinmeters\n\n");
                length_to_metric();
            }
            if(b == 2)
            {
                printf("You have chosen to convert meters/centimeters to feet/inches\n\n");
                length_to_us();
            }
        }
    }while(b != 0);
}
void convert_weight(int a)
{
    int b;
    do
    {

        if(a == 2)
        {
           printf("You have chosen to convert weights\n");
           printf("What units of weight do you want to convert?\n");
           printf("- 1 for pounds/ounces to kilograms/grams\n");
           printf("- 2 for kilograms/gram to pounds/ounces\n");
           printf("- 0 to go back to other options\n");
           scanf("%b", &b);
           if(b == 1)
           {
               printf("You have chosen to convert pounds/ounces to kilograms/gram\n\n");
               weight_to_metric();

           }
           if(b == 2)
           {
               printf("You have chosen to convert kilograms/gram to pounds/ounces\n\n");
               weight_to_us();
           }
        }

    }while(b != 0);
}
void length_to_metric(void)
{
    return;
}
void length_to_us(void)
{
    return;
}
void weight_to_metric(void)
{
    return;
}
void weight_to_us(void)
{
    return;
}

【问题讨论】:

  • 想想当a != 2 in convert_weight - b 不保证为0时会发生什么
  • 当然length_to_us 应该是length_to_imperial。也不需要那些回报。

标签: c function loops stub infinite


【解决方案1】:

您在convert_weight 函数中为scanf 使用了错误的格式说明符:

scanf("%b", &b);
        ^

应该是%d,读取一个整数。

【讨论】:

  • 啊,是的,谢谢!当我查看我的函数时,我似乎得到了隧道视野,结果证明通常是 scanf 的问题。
  • 您应该将那些 if(a==2) 测试从您的循环中取出。如果您要调用函数并提供错误的参数,您将得到一个无限循环。这种情况下的参数似乎完全没有意义。
  • 但是,在我输入正确的参数之前,这些方法不能重复循环吗?我的意思是通过 do-while 循环
  • 你永远不会在这些函数中修改a。如果a 的值错误,则循环将无限循环,因为您永远不会输入读取b 值的代码块(暂时忽略它也是未定义的行为,因为b 未初始化)。跨度>
  • 哦,再次感谢,哈哈,我明白了,因为我通过用户输入调用了函数,如果有 if 语句也没关系,真的可以解决问题。
猜你喜欢
  • 2014-01-04
  • 1970-01-01
  • 2022-01-23
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
相关资源
最近更新 更多