C++程序停止响应
C++ Program stopped responding
我做了一个运行的程序,我可以输入三个变量,长度,外径和内径。输入内径后,程序冻结,并弹出一个窗口,说程序已停止响应。该程序的目的是在给定多个可变输入的情况下计算圆柱形管道的面积、体积、质量、重量等。任何帮助将不胜感激。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159
#define ACC_GRAVITY 9.81 /* metres/sec^2 */
#define FALSE 0
#define TRUE !FALSE
int main(void)
{
/* define the required variables */
float length;
float ext_diam, int_diam;
float ext_rad, int_rad;
float volume;
float weight;
float area;
float mass;
float width;
float flag_1, flag_2, thickness, percent;
double no_sheets;
/* define some constants - could use #define */
const float density_convert = 1.0E3;
const float mm_to_metres = 1.0E-3;
const float density = 8.03; /* grams per cm^3 */
/* prompt and get values */
printf("input the length of pipe in metres: ");
scanf("%f", &length);
do
{
flag_1 = FALSE;
flag_2 = FALSE;
printf("intput the external diameter of the pipe in milimeters: ");
scanf("%f", &ext_diam);
printf("ext_diam: %fn", ext_diam);
printf("intput the internal diameter of the pipe in milimeters: ");
scanf("%f", int_diam);
printf("int_diam: %fn", int_diam);
if (ext_diam < int_diam)
{
printf("external diameter must be greater than the internal diametern");
flag_1 = TRUE;
}
else
{
percent = thickness / ext_diam * 100.0;
if(percent > 2.5 )
{
/* do calculations - conversions, area of pipe cross-section, volume
** of pipe, mass and weight of pipe */
ext_diam = ext_diam * mm_to_metres;
int_diam = int_diam * mm_to_metres;
ext_rad = ext_diam / 2.0;
int_rad = int_diam / 2.0;
area = (PI * ext_rad * ext_rad) - (PI * int_rad * int_rad);
volume = area * length;
mass = volume * density * density_convert;
weight = mass * ACC_GRAVITY;
}
else
{
printf("Width of pipe too smalln");
flag_2 = TRUE;
}
}
} while(ext_diam > 0.0);
/* output the results */
printf("area of cylinder: %f m^2n", area);
printf("volume of steel needed: %f m^3n", volume);
printf("mass of steel needed: %f kgn", mass);
printf("weight of steel needed: %f newtonsn", weight);
/* compute number of sheets of steel needed - 10m is max length of a
** sheet */
no_sheets = trunc (length / 10.0) + 1;
printf("number of 10m long sheets needed: %dn", no_sheets);
/* assume width of sheets is based on the average of the internal and
** external diameters */
width = 2.0 * PI * (ext_rad + int_rad) / 2.0;
printf("width of sheets: %f mn",width);
system("pause");
return 0;
}
您的代码存在一些问题:
-
scanf("%f", int_diam);
,scanf 需要一个指向变量的指针,这将起作用scanf("%f", &int_diam);
-
在行中
percent = thickness / ext_diam * 100.0;
第一次使用时没有初始化厚度(你忘记了一段话吗?
不确定是否打算在ext_diam
为 <= 0 时终止。
最后,存在多个双精度到浮点转换(例如面积演算),这可能会导致数据丢失
将scanf("%f", int_diam);
更改为scanf("%f", &int_diam);
此外,您的do while
循环似乎进入了一个无限循环。由于它取决于外径,因此请更改环路条件,或确保外径在环内达到值 <=0。仅当用户输入 ext_diam
的非正值时,您的do while
循环才会终止。
此外,假设thickness
是指管道的厚度,请在使用此变量 thickness=ext_diam-int_diam;
或用于计算厚度的任何其他公式之前添加此公式。
相关文章:
- 应用程序在打开的简历中捕获视频后没有响应
- 如何使用从处理程序调度的最终回调将响应异步返回给调用方on_read?
- 问题导致程序停止响应,并且姓氏未正确打印
- Qt应用程序不响应鼠标和键盘事件
- 如何让 c++ 程序执行以响应文件打开
- 前和后条件用法。为什么程序不响应条件
- 当后端响应需要很长时间时,WXWIDGET应用程序会挂起
- 有没有一种方法可以测量桌面应用程序 UI 响应时间
- 在 UWP 应用程序中,future.wait() 在尝试同步来自异步方法的响应时继续等待
- X11应用程序在Ubuntu中未响应
- 编译的程序在其他窗口PC上停止响应
- 应用程序使用QMAP存储对象时停止响应
- Win32 中的 Sleep() 使程序无响应
- C++的新手,程序在取消引用向量的一部分时没有响应
- 处理Windows消息,以便我的应用程序正确响应
- 需要接受用户的响应才能执行程序
- Linux:应用程序响应能力和select()
- C++程序停止响应
- 矩阵的动态分配使程序不响应
- 应用程序的响应时间与给定时间段内输入触发器的数量不经意地相关