0

2 つの比較的同じスタブ関数がありますが、2 番目のスタブを呼び出すと、無限ループのように見え、その理由がわかりません。無限ループするこのコードで呼び出している関数は 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;
}
4

1 に答える 1