Почему моя программа не заканчивается после ввода измерений для области определенной формы? - PullRequest
0 голосов
/ 11 октября 2018

У моей программы проблемы.Он проходит через функцию, но просто продолжает повторяться.Это на самом деле не бесконечный цикл, но он возвращается туда, где он просит измерения.

Я довольно сильно озадачен завершением программы.

#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;}

Я изменил все "while" в моей главной функции int на "if", но теперь возникла новая проблема,Окно отладки просто закрывается после ввода измерений.

Редактировать: Спасибо за помощь!Я изменил «время» на «если», и он не выводил.Я только что понял, что моя системная пауза была после моего возвращения 0;и это было проблемой.Теперь все работает отлично!

1 Ответ

0 голосов
/ 11 октября 2018

В вашей функции main() у вас есть:

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

Но вы никогда не измените shapeType внутри цикла, поэтому он продолжает повторяться.

Вам не нужен цикл там ... вам нужна серия if операторов, чтобы выбрать правильную форму и обработать ее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...