一些简单的逻辑问题并打印一些东西 C++ HouseWindowsLab
Issue with some simple logic and getting something to print C++ HouseWindowsLab
请帮忙。这个简单的程序假设计算用户输入他们想要的每栋房子的数量。将它乘以每个房子增加的 windows。并将 1% windows 应用到备件总数。为什么备件不工作?
/*************************************
* 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 */
你的逻辑完全没问题。这只是一个很小的语法错误。
在这一行中:
printf("The spare windows needed &d are windows.\n",SpareWindows);
&d
应替换为 %d
。
我想你只是忽略了它,因为其他部分都很完美。
你的问题是整数不能在点后有数字。例如: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*/
由于 windows 的总数可以始终只是一个 "full" 数字,因此将它们声明为 ints
是完全可以的。但是当移动到百分比时,比如 SpareWindows
,你必须使用 floats
。你可能想看看here
试试这个代码:
#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 */
这里实际上的问题是,当您尝试将整数乘以小数时,输出会转换为整数,因此我们必须明确指定该值应为浮点数。
希望有用。
您的程序中存在多个问题。
下面我开始一一指出来
考虑:scanf("\n%d", &MontgomeryHouses);
为什么您在 scanf()
中使用了 '\n'
?此处输入时无需使用'\n'
。
我看到您在程序中使用了 fflush(stdin);
5 次。停止在您的 C 程序中使用 fflush(stdin)
。 fflush()
函数只能用于输出流。阅读 Standard streams to know more information about this. Use of fflush(stdin)
is an Undefined behavior in both C & C++. Read the answers of these questions What does fflush(stdin) do in C programing? & Using fflush(stdin) 了解更多信息。
另一个问题是你写了SpareWindows = TotalWindows * 0.01;
。您想要在 SpareWindows
变量中使用浮点值,但该变量的数据类型被定义为 int
类型。它应该被定义为 float SpareWindows;
所以你必须使用 '%f'
格式说明符来打印这个变量的值。
请帮忙。这个简单的程序假设计算用户输入他们想要的每栋房子的数量。将它乘以每个房子增加的 windows。并将 1% windows 应用到备件总数。为什么备件不工作?
/*************************************
* 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 */
你的逻辑完全没问题。这只是一个很小的语法错误。 在这一行中:
printf("The spare windows needed &d are windows.\n",SpareWindows);
&d
应替换为 %d
。
我想你只是忽略了它,因为其他部分都很完美。
你的问题是整数不能在点后有数字。例如: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*/
由于 windows 的总数可以始终只是一个 "full" 数字,因此将它们声明为 ints
是完全可以的。但是当移动到百分比时,比如 SpareWindows
,你必须使用 floats
。你可能想看看here
试试这个代码:
#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 */
这里实际上的问题是,当您尝试将整数乘以小数时,输出会转换为整数,因此我们必须明确指定该值应为浮点数。 希望有用。
您的程序中存在多个问题。
下面我开始一一指出来
考虑:scanf("\n%d", &MontgomeryHouses);
为什么您在 scanf()
中使用了 '\n'
?此处输入时无需使用'\n'
。
我看到您在程序中使用了 fflush(stdin);
5 次。停止在您的 C 程序中使用 fflush(stdin)
。 fflush()
函数只能用于输出流。阅读 Standard streams to know more information about this. Use of fflush(stdin)
is an Undefined behavior in both C & C++. Read the answers of these questions What does fflush(stdin) do in C programing? & Using fflush(stdin) 了解更多信息。
另一个问题是你写了SpareWindows = TotalWindows * 0.01;
。您想要在 SpareWindows
变量中使用浮点值,但该变量的数据类型被定义为 int
类型。它应该被定义为 float SpareWindows;
所以你必须使用 '%f'
格式说明符来打印这个变量的值。