【发布时间】:2021-04-02 01:40:15
【问题描述】:
我试图让程序能够一遍又一遍地重复我似乎无法让 while 循环工作,我不确定如何使用 while 循环来构造它。该程序计算不同物体之间的热传递。我已经尝试在开头添加一个while循环,但程序仍然没有重复
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
//Variables
char userName [50];
int choice; /*Material picked by the user*/
double thickness; /* "th" Thickness of the material in question*/
double surfaceArea; /* "a" Surface Area of the wall in ft^2*/
double temp1; /* "t1" Temperature of wall 1 in Fahrenheit*/
double temp2; /* "t2" Temperature of wall 2 in Fahrenheit*/
double heatTransfer; /* "h" The heat transfer rate in BTU/hr*/
/*Variable k, which is a constant, will be defined for each material in the materials own processing*/
//Prompting for Name
printf("Hello, welcome to the Heat Trasnfer Program! Could you please input your name: "); /*prompting name of user*/
scanf("%s", userName);
printf("\nHello %s, with what can I help you today? The menu is down below:\n\n", userName);/*prompting to get them ready for the menu.*/
//Menu that's displayed on screen
printf("Heat Transfer Menu:\n");
printf("\tMaterials:\t\t \n");
printf("\t1. Hardwood Siding\t\t \n");
printf("\t2. Concrete Block\t\t\n");
printf("\t3. Styrofoam\t\t\t \n");
printf("\t4. Fiberglass\t\t\t \n");
printf("\t5. Drywall\t\t\t \n");
printf("\t6. Quit.\n\n");
//Gathering Material that will be worked with
printf("Please input your material choice number: ");
scanf("%d", &choice);
//Testing Choice to see if it's a valid input
if (choice == 1)
{
printf("\nYou chose the material Hardwood Siding.\n");/*Verifying that the material is correct*/
printf("\nWhat is the thickness of the Hardwood Siding in feet?: ");
scanf("%lf", &thickness);
printf("What is the surface area of the Hardwood Siding wall in ft?: ");
scanf("%lf", &surfaceArea);
printf("What about the temperature of wall 1 in Fahrenheit?: ");
scanf("%lf", &temp1);
printf("What about the temperature of wall 2 in Fahrenheit?: ");
scanf("%lf", &temp2);
//Processing the Heat Transfer of Hardwood Siding with a k of 0.092
heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.092));
printf("\nThe heat transfer rate of Hardwood Siding is %lf BTU/hr.", heatTransfer);
}
else if (choice == 2)
{
printf("\nYou chose the material Concrete Block.\n");
printf("\nWhat is the thickness of the Concrete Block in feet?: ");
scanf("%lf", &thickness);
printf("What is the surface area of the Concrete Block wall in ft?: ");
scanf("%lf", &surfaceArea);
printf("What about the temperature of wall 1 in Fahrenheit?: ");
scanf("%lf", &temp1);
printf("What about the temperature of wall 2 in Fahrenheit?: ");
scanf("%lf", &temp2);
//Processing the Heat Transfer of Concrete Block with k 0.260
heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.260));
printf("\nThe heat transfer rate of Concrete Block with a thickness of %lf, a surface area of %lf, and with temperatures of %lf and %lf is %lf BTU/hr.", thickness, surfaceArea, temp1, temp2, heatTransfer);
}
else if (choice == 3)
{
printf("\nYou chose the material Styrofoam.\n");
printf("\nWhat is the thickness of the Styrofoam in feet?: ");
scanf("%lf", &thickness);
printf("What is the surface area of the Styrofoam wall in ft?: ");
scanf("%lf", &surfaceArea);
printf("What about the temperature of wall 1 in Fahrenheit?: ");
scanf("%lf", &temp1);
printf("What about the temperature of wall 2 in Fahrenheit?: ");
scanf("%lf", &temp2);
//Processing the Heat Transfer of Styrofoam with a k of 0.017
heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.017));
printf("\nThe heat transfer rate of Styrofoam with a thickness of %lf, a surface area of %lf, and with temperatures of %lf and %lf is %lf BTU/hr.", thickness, surfaceArea, temp1, temp2, heatTransfer);
}
else if (choice == 4)
{
printf("\nYou chose the material Fiberglass.\n");
printf("\nWhat is the thickness of the Fiberglass in feet?: ");
scanf("%lf", &thickness);
printf("What is the surface area of the Fiberglass wall in ft?: ");
scanf("%lf", &surfaceArea);
printf("What about the temperature of wall 1 in Fahrenheit?: ");
scanf("%lf", &temp1);
printf("What about the temperature of wall 2 in Fahrenheit?: ");
scanf("%lf", &temp2);
//Processing the Heat Transfer of Fiberglass with a k of 0.027
heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.027));
printf("\nThe heat transfer rate of Fiberglass with a thickness of %lf, a surface area of %lf, and with temperatures of %lf and %lf is %lf BTU/hr.", thickness, surfaceArea, temp1, temp2, heatTransfer);
}
else if (choice == 5)
{
printf("\nYou chose the material Drywall.\n");
printf("What is the thickness of the Drywall in feet?: ");
scanf("%lf", &thickness);
printf("What is the surface area of the Drywall wall in ft?: ");
scanf("%lf", &surfaceArea);
printf("What about the temperature of wall 1 in Fahrenheit?: ");
scanf("%lf", &temp1);
printf("What about the temperature of wall 2 in Fahrenheit?: ");
scanf("%lf", &temp2);
//Processing the Heat Transfer of Drywall with a k of 0.093
heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.093));
printf("\nThe heat transfer rate of the Drywall with a thickness of %lf, a surface area of %lf, and with temperatures of %lf and %lf is %lf BTU/hr.", thickness, surfaceArea, temp1, temp2, heatTransfer);
}
else if (choice == 6)
{
printf("\nThank you! Bye bye %s.\n", userName);
}
//Output
printf("\n\n\nThank you for using this program %s, bye bye.", userName);
return 0;
}
【问题讨论】:
-
您必须 始终检查scanf返回的值。如果输入流包含“a”,
int choice; scanf("%d", &choice); if( choice ==1 )是未定义的行为,因为选择未初始化且 scanf 未分配给它。
标签: c loops while-loop