Я не могу выполнить программу, которая включает пользовательский ввод целого числа внутри оператора else if - PullRequest
0 голосов
/ 23 июня 2019

Когда пользователь вводит 1,2 или 3, программа завершает работу, не сканируя количества внутри блока операторов else if.

Как сканировать внутри оператора if или else if?

if (num==4)
{
    printf("exiting");
    goto end;
}

else
{
    if (num=='1')
    {
        printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*kmstomiles;
        printf("the value of the second unit is %f",second);
    }  

     else if (num=='2')
    {
         printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*inchestofeet;
        printf("the value of the second unit is %f",second);

    }
    else if (num=='3')
    {
        printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*cmtoinches;
        printf("the value of the second unit is %f",second);
    }

1 Ответ

1 голос
/ 23 июня 2019

Пожалуйста, используйте ниже код,

if (num==4)
{
    printf("exiting");
    goto end;
}

else
{
    if (num==1)
    {
        printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*kmstomiles;
        printf("the value of the second unit is %f",second);
    }  

     else if (num==2)
    {
         printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*inchestofeet;
        printf("the value of the second unit is %f",second);

    }
    else if (num==3)
    {
        printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*cmtoinches;
        printf("the value of the second unit is %f",second);
    }
...