views:

127

answers:

3

This program is supposed to judge the height of multiple buildings by the type of building and number of stories. There is a loop that continues to ask the questions until the user enters "0" for type of building. At the end, it prints a report showing the types of buildings and how many of each conform to building codes. I am having problems compiling the program, but I'm not sure if the loop is right either.

#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;
}

Here's the error I get when I try to compile: assign04.c:79: error: expected ‘while’ before ‘printf’ assign04.c:82: error: expected declaration or statement at end of input

Any help would be appreciated.

Updated:

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;
}
+1  A: 

Your braces are unbalanced; you're missing an end brace. It looks like you're missing the end of your do clause.

Also, note that since your scanf statements are outside of any loop, your question will only be asked once. You'll need to ask the user inside a loop. You can also, most likely, avoid having two nested (the do and the while) loops; one should be sufficient.

Michael Petrotta
A: 
while (type != 0)
{
    do
    {

I don't see a } while (condition); that corresponds to the do.

Also, it's unrelated to your compilation error, but you should avoid using scanf unless you know what you're doing: http://c-faq.com/stdio/scanfprobs.html

jamesdlin
+1  A: 

A While loop is of the form:

while(condition)
{
    body of code
}

And a do while is of the form:

do
{
    body of code
} while(condition);

The difference is that the do while loop guarantees the body of the loop will execute at least once.

The code you posted is a mix of the two. Figure out which type of while loop you need for this particular problem as this appears to be homework.

Evan
correction: "body of the loop will execute _at least_ once"
San Jacinto
Ok, thanks for the explanation. I believe I've corrected the code, but I'm still getting the error: "assign04.c:74: error: expected ‘;’ before ‘printf’" while trying to compile.
Jesse
@Jesse: that indicates that the compiler is looking for the end of the `do` loop (`while(condition);`), but not finding it. Do you still have a `do` statement? If so, do you have a corresponding `while(condition);`? (note the semicolon!)
Michael Petrotta
@Jesse: also, please fix the compiler message corruption. We see `expected ‘;’ before ‘printf’"`. Makes it hard to see what's actually going on.
Michael Petrotta
I changed the code to the second example above. So there is a do statement, and all of the { are closed.
Jesse
... that's how it's showing up in JGrasp. I'm not really sure how to fix that.
Jesse
@Jesse: dunno. Post your updated code.
Michael Petrotta
Do I need to put an "else" statement, even if I don't want anything to happen if the conditions of "if" aren't met?
Jesse
Updated code posted above.
Jesse
Michael Petrotta
Thanks, I will probably go get it. I am doing this for a class, and the teacher assumes alot. She is not much of a teacher, and it's obviously showing. I'm sure I'm just missing some simple character somewhere, but it gets hard to notice stuff like that, when you've been staring at code for a few hours.
Jesse
@Jesse: you're missing a closing parenthesis on the last `printf`.
Michael Petrotta
I understand. Do yourself a favor and go buy that book. It's not only a great way to learn C, it's also 1) one of the best introductory CS textbooks ever written, and 2) really short.
Michael Petrotta
Ok, I found the problem. Of course, I missed a ) after the last printf statement. I'm going to buy the book tomorrow.
Jesse