Domanda

I have two relatively the same stub functions and yet when i call the second stub it seems to infinite loop and i am not sure why. The function that i am calling in this code that infinite loops is convert_weight();. I have my doubts as to if it is a main program problem, since it gets called in and prints out the first few printfs without problem but when user input from scanf is used, it goes into infinite loop. Any help would be useful, please and thanks.

#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;
}
È stato utile?

Soluzione

You used the wrong format specifier for scanf in the convert_weight function:

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

It is supposed to be %d, which reads an integer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top