一些简单的逻辑问题,并得到了一些可以打印的C ++ HouseWindowsLab

时间:2018-09-07 04:45:42

标签: c stdio

请帮助。假设此简单程序计算用户想要的每座房屋的数量的输入。将其乘以每个房屋添加的窗户。并为备用磁盘增加1%的窗口总数。为什么零件不起作用?

/*************************************
* PROGRAM: WindowCalculator
* AUTHOR: Matthew Nickles
* DATE: 9/6/2018
* NOTES: This is for educational purposes, the program calculates
* how many windows for each house plan a city builder would get.
**************************************/

/* PREPROCESSOR COMMANDS */
#include <stdio.h>


/* MAIN PROCESSING CONTROL */
int main()
{
/* VARIABLE DECLARATIONS */
int MontgomeryHouses; /*20 Windows per a house*/
int KetteringHouses; /*15 Windows per a house*/
int SaxonHouses; /*12 Windows per a house*/
int TotalWindows; /*Total calculation of all windows*/
int SpareWindows; /*How many spare windows if you wanted 1% of total*/ 

/* WINDOWS PER HOUSE ALGORITHM */
printf("\n How many Montgomery Houses do you wish to build? They have 20             windows. ");
scanf("\n%d", &MontgomeryHouses);
fflush(stdin);

printf("\n How many Kettering Houses do you wish to build? They have 15 windows. ");
scanf("\n%d", &KetteringHouses);
fflush(stdin);

printf("\n How many Saxon Houses do you wish to build? They have 12 windows. ");
scanf("\n%d", &SaxonHouses);
fflush(stdin);

/* CALCULATE TOTAL WINDOWS*/
TotalWindows = (MontgomeryHouses * 20) + (KetteringHouses * 15)
        + (SaxonHouses * 12);
SpareWindows = TotalWindows * 0.01;
/* DISPLAY OUTPUT*/
printf("The spare windows needed &d are windows.\n",SpareWindows);
fflush(stdin);  
printf("\nThe total amount of windows needed for all houses are %d windows.\n",TotalWindows);
fflush(stdin);



return 0;
}
/* END OF PROGRAM */

4 个答案:

答案 0 :(得分:5)

您的逻辑完全没问题。这只是一个很小的语法错误。 在这一行:

printf("The spare windows needed &d are windows.\n",SpareWindows);

&d应该替换为%d

我认为您只是忽略了它,因为其他部分都很完美。

答案 1 :(得分:0)

您的问题是,整数不能在点之后有数字。例如:SpareWindows = 50.43,但是由于您将其声明为int,因此它被截断为50。您可能想改用floats,例如:

/* VARIABLE DECLARATIONS */
int MontgomeryHouses; /*20 Windows per a house*/
int KetteringHouses; /*15 Windows per a house*/
int SaxonHouses; /*12 Windows per a house*/
int TotalWindows; /*Total calculation of all windows*/
float SpareWindows; /*How many spare windows if you wanted 1% of total*/ 

由于窗口的总数始终只能是一个“完整”的数字,因此将它们声明为ints非常好。但是,当移动到某个百分比(例如SpareWindows)时,您必须使用floats。您可能想看看here

答案 2 :(得分:0)

尝试以下代码:

#include <stdio.h>


/* MAIN PROCESSING CONTROL */
int main()
{
/* VARIABLE DECLARATIONS */
int MontgomeryHouses; /*20 Windows per a house*/
int KetteringHouses; /*15 Windows per a house*/
int SaxonHouses; /*12 Windows per a house*/
int TotalWindows; /*Total calculation of all windows*/
float SpareWindows; /*How many spare windows if you wanted 1% of total*/ 

/* WINDOWS PER HOUSE ALGORITHM */
printf("\n How many Montgomery Houses do you wish to build? They have 20             windows. ");
scanf("\n%d", &MontgomeryHouses);
fflush(stdin);

printf("\n How many Kettering Houses do you wish to build? They have 15 windows. ");
scanf("\n%d", &KetteringHouses);
fflush(stdin);

printf("\n How many Saxon Houses do you wish to build? They have 12 windows. ");
scanf("\n%d", &SaxonHouses);
fflush(stdin);

/* CALCULATE TOTAL WINDOWS*/
TotalWindows = (MontgomeryHouses * 20) + (KetteringHouses * 15)
        + (SaxonHouses * 12);
SpareWindows = (float)TotalWindows * 0.01; //Used Explicit type conversion to float
/* DISPLAY OUTPUT*/
printf("The spare windows needed %f are windows.\n",SpareWindows);
fflush(stdin);  
printf("\nThe total amount of windows needed for all houses are %d windows.\n",TotalWindows);
fflush(stdin);



return 0;
}
/* END OF PROGRAM */

实际上,这里的问题是当您尝试将整数乘以小数时,输出将转换为整数,因此我们必须明确指定该值应为浮点数。 希望能奏效。

答案 3 :(得分:0)

程序中存在多个问题。

让我开始一一指出它们。

考虑:scanf("\n%d", &MontgomeryHouses);

为什么在'\n'中使用了scanf()?进行输入时,无需在此处使用'\n'

我发现您在程序中使用了fflush(stdin); 5次。停止在C程序中使用fflush(stdin)fflush()函数仅应用于输出流。阅读Standard streams,以了解有关此的更多信息。在C和C ++中,fflush(stdin)的使用是Undefined behavior。阅读这些问题What does fflush(stdin) do in C programing?Using fflush(stdin)的答案以了解更多信息。

另一个问题是您写了SpareWindows = TotalWindows * 0.01;。您想在SpareWindows变量中使用浮点值,但是此变量的数据类型定义为int类型。应该将其定义为float SpareWindows;,因此您必须使用'%f'格式说明符来打印此变量的值。