0

计算几何图形的面积和周长。首先要求用户输入代表形状的字母。我们用 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);
}
4

1 回答 1

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
于 2013-10-18T19:04:21.700 回答