While loop help

不羁的心 提交于 2019-12-07 15:28:41

问题


Hey guys... its the newbie again :) I'm putting together a program that will calculate the area of either a triangle or a square and then prompt the user whether they wish to calculate another. I've got the code working to the point that it will calculate the area of either shape, but then doesn't continue with the rest of the code. For example is square is selected, the area is calculated and then goes back to the prompt for the square's side. I'm assuming that it's again the while looping forever, but don't know how to stop the loop from going on endlessly.

Heres my code:

 #include<stdio.h>
 #include<math.h>

 int main(void)

 {
     float sq_side, tri_base, tri_height, Area;
     char shape, cont = 'Y';

     printf("Please select the type of shape you desire to calculate the area for:\n");
     printf("                                                                     \n");
     printf("   Square = S                             Triangle = T               \n");
     printf("   -------                                    x                      \n");
     printf("   :     :                                   x x                     \n");
     printf("   :     :                                  x   x                    \n");
     printf("   -------                                 xxxxxxx                   \n");
     printf("                                                                     \n");
     printf("Please select either S or T:");
     scanf("%c", &shape);
     while (cont != 'n' && cont != 'N')

        if (shape == 'S' || shape == 's')
        {
            printf("What is the length of the sides of the square?:\n");
            scanf("%f", &sq_side);

            Area = pow(sq_side,2);

            printf("The area of the square is %.2f.\n", Area);
        }   

        else if (shape == 'T' || shape == 't')
        {
            printf("What is the length of the base of the triangle?:\n");
            scanf("%f", &tri_base);
            printf("What is the height of the triangle?:\n");
            scanf("%f", &tri_height);

            Area = 0.5 * tri_base * tri_height;

            printf("The area of the triangle is %.2f.\n", Area);
        }   

        else 
        {
        printf("Error:  You have select an incorrect option.");
        }

        printf("Do you wish to calculate a new shape?");
        fflush(stdin);
        scanf("%c", &cont);

     return(0);

 }

回答1:


You're missing curly braces. The result was that only the if statement (which includes the chain of else ifs) was actually in the loop body. The printf (and later) is not part of this compound statement.

 while (cont != 'n' && cont != 'N')
 {
    if (shape == 'S' || shape == 's')
    {
        printf("What is the length of the sides of the square?:\n");
        scanf("%f", &sq_side);

        Area = pow(sq_side,2);

        printf("The area of the square is %.2f.\n", Area);
    }   

    else if (shape == 'T' || shape == 't')
    {
        printf("What is the length of the base of the triangle?:\n");
        scanf("%f", &tri_base);
        printf("What is the height of the triangle?:\n");
        scanf("%f", &tri_height);

        Area = 0.5 * tri_base * tri_height;

        printf("The area of the triangle is %.2f.\n", Area);
    }   

    else 
    {
    printf("Error:  You have select an incorrect option.");
    }

    printf("Do you wish to calculate a new shape?");
    fflush(stdin);
    scanf("%c", &cont);
}   



回答2:


Theres no brackets for you while loop. So the code outside of your if elseif else block is not being called.

currently your code translates to

while (cont != 'n' && cont != 'N')
{
    if (shape == 'S' || shape == 's')
    {}
    else if (shape == 'T' || shape == 't')
    {}   
    else 
    {}
}

printf("Do you wish to calculate a new shape?");
fflush(stdin);
scanf("%c", &cont);

when you want

while (cont != 'n' && cont != 'N')
{
    if (shape == 'S' || shape == 's')
    {}
    else if (shape == 'T' || shape == 't')
    {}   
    else 
    {}

    printf("Do you wish to calculate a new shape?");
    fflush(stdin);
    scanf("%c", &cont);
}

Remember that if you don't include curly braces in a control structure, it only calls the next statement, which in your case is a series of nested if-else if statements.

Hope it helps - Val



来源:https://stackoverflow.com/questions/3714269/while-loop-help

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!