C哨兵循环问题

时间:2010-09-24 00:49:16

标签: c while-loop

该程序应根据建筑类型和故事数来判断多栋建筑的高度。有一个循环继续询问问题,直到用户输入“0”表示建筑类型。最后,它会打印一份报告,显示建筑物的类型以及每个建筑物的数量符合建筑规范的数量。我在编译程序时遇到问题,但我不确定循环是否正确。

#include <stdio.h> 
#include <math.h>
//constants
#define MIN_HEIGHT 180
#define MAX_HEIGHT 220
#define ROOF_MULT 2.0

int main()
{
    //variables
    int type, stories, F_TO_MECH, osum, rhsum, msum;
    double height, ADD_MECH_HEIGHT, code, F_HEIGHT;
    osum=0, rhsum=0, msum=0;


    //Find type of building and number of stories.
    printf("Enter a building type (1=Office, 2=Residential/Hotel, 3=Mixed-Use, 0=Stop): ");
    scanf("%d", &type);
    printf("Enter the number of stories in the building: ");
    scanf("%d", &stories);


    //Switch to differentiate constants of building types.
    while (type != 0)
    {
        do
        {
            switch (type) //Switch for building constants.
            {
                case 1:     F_HEIGHT=3.9;
                            ADD_MECH_HEIGHT=2.0;
                            F_TO_MECH=20;
                            break;

                case 2:     F_HEIGHT=3.1;
                            ADD_MECH_HEIGHT=1.55;
                            F_TO_MECH=30;
                            break;

                case 3:     F_HEIGHT=3.5;
                            ADD_MECH_HEIGHT=1.75;
                            F_TO_MECH=25;
                            break;
            }

            //Formula to find height.
            height = (stories * F_HEIGHT) + ((F_HEIGHT * ROOF_MULT) + ADD_MECH_HEIGHT) + (ADD_MECH_HEIGHT * (stories / F_TO_MECH));


            if( height <= MAX_HEIGHT )
            {
                if( height >= MIN_HEIGHT )
                {
                    switch (type)
                    {
                        case 1: osum = osum++;

                        case 2: rhsum = rhsum++;

                        case 3: msum = msum++;
                    }
                }
            }
                printf("Enter a building type (1=Office, 2=Residential/Hotel, 3=Mixed-Use, 0=Stop): ");
                scanf("%d", &type);
                printf("Enter the number of stories in the building: ");
                scanf("%d", &stories);

        } //End While statment.
    //print results.
    printf("Building Type   Count \n-------------------\nOffice         %3.0f\nRes/Hotel      %3.0f\nMix-Use        %3.0f\n", osum, rhsum, msum;

    return 0;
}

这是我尝试编译时遇到的错误: assign04.c:79:错误:在“printf”之前预期“while” assign04.c:82:错误:输入结束时的预期声明或声明

任何帮助都将不胜感激。

更新:

int main()
{
//variables
int type, stories, F_TO_MECH, osum, rhsum, msum;
double height, ADD_MECH_HEIGHT, F_HEIGHT;
osum=0, rhsum=0, msum=0;

//Find type of building
printf("Enter a building type (1=Office, 2=Residential/Hotel, 3=Mixed-Use, 0=Stop): ");
scanf("%d", &type);

//Switch to differentiate constants of building types.
do
{
    //find the number of stories.
    printf("Enter the number of stories in the building: ");
    scanf("%d", &stories);

    switch (type) //Switch for building constants.
    {
        case 1: F_HEIGHT=3.9;
                    ADD_MECH_HEIGHT=2.0;
                    F_TO_MECH=20;
                    break;

        case 2:     F_HEIGHT=3.1;
                    ADD_MECH_HEIGHT=1.55;
                    F_TO_MECH=30;
                    break;

        case 3: F_HEIGHT=3.5;
                    ADD_MECH_HEIGHT=1.75;
                    F_TO_MECH=25;
                    break;
    }

    //Formula to find height.
    height = (stories * F_HEIGHT) + ((F_HEIGHT * ROOF_MULT) + ADD_MECH_HEIGHT) + (ADD_MECH_HEIGHT * (stories / F_TO_MECH));


    if( height <= MAX_HEIGHT )
    {
        if( height >= MIN_HEIGHT )
        {
            switch (type)
            {
                case 1: osum = osum++;

                case 2: rhsum = rhsum++;

                case 3: msum = msum++;
            }
        }
    }

} while (type != 0); //End While statment.

//print results.
printf("Building Type   Count \n-------------------\nOffice         %3.0f\nRes/Hotel      %3.0f\nMix-Use        %3.0f\n", osum, rhsum, msum;

return 0;
}

3 个答案:

答案 0 :(得分:1)

你的牙套是不平衡的;你错过了一个结束支架。看起来你错过了do条款的结尾。

另请注意,由于您的scanf语句不属于任何循环,因此您的问题只会被问到一次。您需要向用户内部询问一个循环。您也可以避免使用两个嵌套(dowhile)循环;一个就足够了。

答案 1 :(得分:1)

While循环的形式为:

while(condition)
{
    body of code
}

还有一段时间的形式:

do
{
    body of code
} while(condition);

不同之处在于do while循环保证循环体至少执行一次。

您发布的代码是两者的混合。找出这个特定问题需要哪种类型的while循环,因为这似乎是作业。

答案 2 :(得分:0)

while (type != 0)
{
    do
    {

我没有看到与} while (condition);对应的do

此外,它与您的编译错误无关,但您应该避免使用scanf,除非您知道自己在做什么:http://c-faq.com/stdio/scanfprobs.html