This program is supposed to judge the height o开发者_运维知识库f 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;
}
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.
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.
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
精彩评论