为什么我的程序在输入某个形状的面积的测量值后没有结束?
Why does my program not end after I input the measurements for the area of a certain shape?
我的程序有问题。它贯穿整个函数,但它只是不断重复。它实际上不是一个无限循环,但它回到了它要求测量的地方。
我对程序结束感到非常困惑。
#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
语句来选择正确的形状并对其进行处理。
相关文章:
- 在C++中测量postscript字体宽度
- 为什么在这个代码结束循环中没有得到结束
- 试图对缓存进行跨步测试,但程序并没有结束
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 为什么擦除方法会影响结束方法
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 用时钟测量程序的运行时间
- 删除映射和分割错误中的一个过去结束元素
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 用于测量微秒的QueryPerformanceCounter的奇怪行为
- 为什么我的两个 cin 语句没有在程序结束时运行?
- 在函数结束后使用指向变量的指针是否安全?
- 为什么根据测量位置测量经过的时间时会有很大的不同?
- C ++尝试并捕获未结束的程序
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 取消引用结束指针到数组类型的一个
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- 结束另一个线程中使用的对象的生存期
- 全局向量导致 C++ 程序结束时出现段错误