C++程序停止响应

C++ Program stopped responding

本文关键字:响应 程序 C++      更新时间:2023-10-16

我做了一个运行的程序,我可以输入三个变量,长度,外径和内径。输入内径后,程序冻结,并弹出一个窗口,说程序已停止响应。该程序的目的是在给定多个可变输入的情况下计算圆柱形管道的面积、体积、质量、重量等。任何帮助将不胜感激。

#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;
}

您的代码存在一些问题:

  1. scanf("%f", int_diam); ,scanf 需要一个指向变量的指针,这将起作用

    scanf("%f", &int_diam);
    
  2. 在行中

    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; 或用于计算厚度的任何其他公式之前添加此公式。