为啥我输入某个形状区域的测量值后程序没有结束?

Posted

技术标签:

【中文标题】为啥我输入某个形状区域的测量值后程序没有结束?【英文标题】:Why does my program not end after I input the measurements for the area of a certain shape?为什么我输入某个形状区域的测量值后程序没有结束? 【发布时间】:2018-10-11 08:12:25 【问题描述】:

我的程序有问题。它贯穿整个函数,但它只是不断重复。这并不是一个真正的无限循环,但它会回到它要求测量的地方。

我对程序结束感到很困惑。

#include <iostream>
#include <iomanip>
using namespace std;

char getShapeType();    
double getAreaOfRectangle(double, double);    
double getAreaOfCircle(double, double);    
double getAreaOfTriangle(double, double);    
double getAreaOfSquare(double);    

char getShapeType()    
char type;

cout << "This program will compute area of a shape.\n"
    << "What is the shape type?\n"
    << "Circle or Rectangle or Square or Triangle? (C or R or S or T): ";
cin >> type;

// Validate the shape type.
while (type != 'C' && type != 'c' &&
    type != 'R' && type != 'r' && type != 'S' && type != 's'
    && type != 'T' && type != 't')

    cout << "Please enter C or R or S or T: ";
    cin >> type;


// Convert lowercase to uppercase.
if (type == 'c')
    type = 'C';

else if (type == 'r')
        type = 'R';

else if (type == 's')
        type = 'S';

else if (type == 't')
        type = 'T';

return type;

int main()   
    
char shapeType;                                 //R=rectangle, T=triangle, C=circle, S= square    
double areaOfRectangle;                         //variable to store the area of rectangle    
double areaOfCircle;                            //variable to store the area of circle    
double areaOfTriangle;                          //variable to store the area of triangle    
double areaOfSquare;                            //variable to store the area of circle    

// Get the shape type.
shapeType = getShapeType();

//Rectangle
while (shapeType == 'R')

    double width;
    double length;

    do
    
        cout << "Enter width and length of rectangle separated by space: " << endl;
        cin >> width >> length;
     while (width <= 0 || length <= 0);

    areaOfRectangle = getAreaOfRectangle(width, length);
    cout << "The area of rectangle with width "
        << width << " and length " << length
        << " is " << areaOfRectangle << endl;



//Circle
while (shapeType == 'C')

    const double PI = 3.14159265359;
    double radius;

    do
    
        cout << "Enter the radius of circle: " << endl;
        cin >> radius;
     while (radius <= 0);

    areaOfCircle = getAreaOfCircle(PI, radius);
    cout << "The area of circle with PI "
        << PI << " and radius " << radius
        << " is " << areaOfCircle << endl;


//Triangle
while (shapeType == 'T')

    double base;
    double height;

    do
    
        cout << "Enter base and height of triangle separated by space: " << endl;
        cin >> base >> height;
     while (base <= 0 || height <= 0);

    areaOfTriangle = getAreaOfTriangle(base, height);
    cout << "The area of triangle with base "
        << base << " and height " << height
        << " is " << areaOfTriangle << endl;


//Square
while (shapeType == 'S')

    double width;

    do
    
        cout << "Enter width of square separated by space: " << endl;
        cin >> width;
     while (width <= 0);

    areaOfSquare = getAreaOfSquare(width);
    cout << "The area of square with width "
        << width
        << " is " << areaOfSquare << endl;




double getAreaOfRectangle(double width, double length)
    
double area = width * length;

return area;


double getAreaOfCircle(double PI, double radius)
    
double area = PI * radius * radius;

return area;


double getAreaOfTriangle(double base, double height)
    
double area = (base * height) / 2;

return area;


double getAreaOfSquare(double width)
    
double area = width * 2;

return area;

我已将 int main 中的所有“while”更改为“if”,但现在出现了一个新问题。输入测量值后,调试窗口就会关闭。

编辑:感谢您的帮助!我将“while”更改为“if”,但它没有输出。我刚刚意识到我的系统暂停是在我返回 0 之后;这就是问题所在。现在它工作得很好!

【问题讨论】:

【参考方案1】:

在您的 main() 函数中,您有:

while (shapeType == 'R')

    /* do stuff */

但你永远不会在循环内更改shapeType,所以它会不断重复。

你不需要一个循环......你需要一系列if 语句来选择正确的形状并处理它。

【讨论】:

我开始明白了。当我将它更改为 if 时,它会阻止它成为一个循环,现在它会变成一个几乎是真的或假的陈述。现在当我输入测量值时调试就关闭了? 亲爱的@Thomas,他的意思是......将 main() 函数中的所有 4 个 while (shapeType == 'R') 更改为 if(shapeType == 'R') 表单...... @p._phidot_ 是的,我已将所有“while”更改为“if”,但现在出现了一个新的完整问题,让我更加难过。现在它不输出任何东西,只是在我输入测量值后关闭。如果我没有任何意义,我深表歉意,这是我的第一堂编程课。 介意分享更新的代码吗? ||对代码进行故障排除的关键之一是..确定哪些部分有效,哪些部分无效。 ||在您的情况下..您可以先将第 55 行注释到第 123 行,然后在第 53 行添加 cout &lt;&lt; shapeType &lt;&lt; endl; 以检查 shapeType 变量是否获得您想要的值.. ||然后对下一节做同样的事情:取消注释、运行、检查。并重复:取消注释、运行、检查。 ||大程序总是建立在小程序之上。这将确保每个小部分都能正常工作。然后你就可以担心大程序的顺序了。

以上是关于为啥我输入某个形状区域的测量值后程序没有结束?的主要内容,如果未能解决你的问题,请参考以下文章

输入字段在设置值后扩展?

为啥鼠标可以变成小手形呢?

为啥我不能以此结束我的输入?

OpenCV vs MATLAB(图像处理的形状测量功能)

如何用cmd强制结束某个程序?

在文本字段中输入值后如何显示*