如何使几何形状的面积和周长起作用?

时间:2013-10-18 18:51:17

标签: c++

计算面积和 几何形状的周长。首先,要求用户输入代表的字母 形状。我们用C代表圆形,R代表矩形,S代表方形 在用户选择形状后,程序会提示相应的形状 因此,形状的尺寸。例如,如果用户选择了正方形, 该计划将要求一方。如果它是一个圆圈,程序将询问半径。如果 它是一个矩形,它会要求长度和宽度。 收到适当的尺寸后,程序将计算面积和 请求形状的周长并将其打印在屏幕上。再一次,代码 会要求另一封信。如果用户输入“Q”,则程序终止。

程序的一次运行将如下所示:

Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit)
>S
Please enter the side of the square 
> 8
The area is 64 and the perimeter is 32
Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit)
>R 
Please enter the width of the rectangle 
> 5 
Please enter the length of the rectangle
> 7  
The area is 35 and the perimeter is 24
Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit) 

这是我到目前为止所做的,但是我不知道为什么当我按下S时,我不能让它要求广场的一面。

我得到的是:

Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit)

除了Q之外我输入的内容重复了同样的问题。 Q只是停止,但输入的任何其他章程都会要求Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit)

发生了什么事?

#include <iostream>

using namespace std;
int main()
{
    //clear the screen.
    //clrscr();
    //declare variable type int
    char shape = 'N'; //none
    int area, perimeter;
    while( shape != 'Q' )
    {
        cout<<"Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit) >"<<endl;
        //get shape choice
        cin>>shape;

        if( shape == 'C' )
        {
        int radius;
        //Circle, radius
        cout<<"Please enter the radius >"<<endl;
        }
        else if( shape == 'S' )
        {
        int side;
        //Input the side
        cout<<"Please enter the side of the square >"<<endl;
        //Square, side
        cin>>side;
        //calculate perimeter and save it in 'peri'
        perimeter=4*side;
        //show the output 'perimeter'
        cout<<"Perimeter of square is "<<perimeter<<endl;
        }
        else if( shape == 'R' )
        {
        int width,length;
        //Rectangle, width,length
        }
    }
    return(0);
}

1 个答案:

答案 0 :(得分:2)

更改您的条件
if (shape == 'R')

if (shape == 'R' || shape == 'r')

或者,在测试之前将它们更改为1个案例:

cin >> shape;
shape = std::toupper(shape);
if (shape == 'R') // already know it is between A-Z, so we can just check the uppercase