为什么我的程序在输入某个形状的面积的测量值后没有结束?

Why does my program not end after I input the measurements for the area of a certain shape?

本文关键字:测量 结束 为什么 输入 程序 我的      更新时间:2023-10-16

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

我对程序结束感到非常困惑。

#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 之后;这就是问题所在。现在它工作得很好!

main()函数中,您有:

while (shapeType == 'R')
{
/* do stuff */
}

但是你永远不会改变循环内的shapeType,所以它会不断重复。

你不需要那里的循环。您需要一系列if语句来选择正确的形状并对其进行处理。