我是这个网站的新手,它告诉我我的帖子主要是代码。这对我的帖子来说并不重要,它就在这里我可以完成帖子参数。查看输出,如何更改我的代码,以便它不会错误地循环。我已经尝试了一切,但我无法弄清楚如何使它工作。请记住,我是一名初学程序员。我不太懂语法。如何使用最简单的语法使这个循环工作?我不知道为什么它会正确循环,然后在for循环之前显示所有内容,然后在else块中显示所有内容。
#include <iostream>
#include <string>
#include <istream>
#include <stdlib.h>
using namespace std;
int main()
{
char repeat = 'n';
string circle;
string triangle;
string rectangle;
string shape;
long double areaCircle;
long double areaTriangle;
long double areaRectangle;
do
{
cout << "Type the shape you want to find the area for.\n";
cout << "Circle\n";
cout << "Triangle\n";
cout << "Rectangle\n";
getline(cin,shape);
if (shape == "circle")
{
long double r;
cout << "Enter the radius of the circle.\n";
cin >> r;
areaCircle = (3.14)*(r*r);
cout << "The area of the circle is " << areaCircle << endl;
cout << "Enter 'Y' to repeat, 'N' to quit\n";
cin >> repeat;
}
else if (shape == "triangle")
{
long double h, b;
cout << "Enter the base.\n";
cin >> b;
cout << "Enter the height.\n";
cin >> h;
areaTriangle = (b*h) / 2;
cout << "The area of the triangle is " << areaTriangle << endl;
cout << "Enter 'Y' to repeat, 'N' to quit\n";
cin >> repeat;
}
else if (shape == "rectangle")
{
long double l, w;
cout << "Enter the length.\n";
cin >> l;
cout << "Enter the width.\n";
cin >> w;
areaRectangle = l * w;
cout << "The area of the rectangle is " << areaRectangle << endl;
cout << "Enter 'Y' to repeat, 'N' to quit\n";
cin >> repeat;
}
else if (shape !=circle || shape!= triangle || shape != rectangle)
{
cout << shape << "is not a valid shape.\n";
cout << "Enter 'Y' to repeat, 'N' to quit\n";
cin >> repeat;
}
}
while (repeat == 'y');
system("pause");
return 0;
}