发布一些简单的逻辑并获得一些东西来打印C ++ HouseWindowsLab

问题描述 投票:1回答:4

请帮忙。这个简单的程序假设计算用户输入他们想要的每个房子的数量。每个房子增加的窗户乘以它。并且对备件的总量应用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 */
c stdio
4个回答
5
投票

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

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

&d应该被%d取代。

我认为你只是忽略了它,因为其他部分是完美的。


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


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 */

实际上问题是当你尝试将整数乘以十进制,输出转换为整数时,所以我们必须明确指定该值应该是一个浮点数。希望有用。


0
投票

您的计划中存在多个问题。

让我开始逐一指出它们。

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

为什么你在'\n'中使用过scanf()?当你接受输入时,没有必要在这里使用'\n'

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

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

© www.soinside.com 2019 - 2024. All rights reserved.