jeudi 1 février 2018

Area of figures(equaliteral triangle, square, penta, hexa, hepta, octa) using if-else (C++)

this is my first C++ programming practice. When I compile this it displays "Enter the length of the side" and "Which figure's area are you calculating? ". I enter 3 and square but the only result I can get is "Unknown figure. Try again." I'm not sure why this happens. maybe something's not connected well..

#include <iostream>
#include<math.h>

using namespace std;

int main()

{
    int Side;
    float Area;
    float sqrt2 = 1.414;
    float sqrt3 = 1.732;
    float sqrt4 = 2;
    float sqrt5 = 2.236;
    float cot = 2.077;
    float pi = 3.141;

    char figure;
    char equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon;

    cout << "Enter the length of the side: " << endl;
    cin >> Side;
    cout << "Which figure's area are you calculating? " << endl;
    cin >> figure;

    if(figure == equaliteral_triangle)
    {
    Area = (sqrt3/4) * (Side * Side);
    cout << "The area of triangle is, "<< Area << endl; 
    }

    else if (figure == square)
    {
    Area =  (Side * Side);
    cout << "The area of square is, "<< Area << endl; 
    }

    else if (figure == pentagon)
    {
    Area = (0.25 *(5 *(5 +(2*sqrt5)))) * (Side * Side);
    cout << "The area of pentagon is, "<< Area << endl; 
    }

     else if (figure == hexagon)
    {
    Area = ((3*sqrt3)/2) * (Side * Side);
    cout << "The area of hexagon is, "<< Area << endl; 
    }

    else if (figure == heptagon)
    {
    Area = (7/4) * (Side * Side) * cot;
    cout << "The area of heptagon is, "<< Area << endl; 
    }

    if (figure == octagon)
    {
    Area = (2 * (1+ sqrt2)) * (Side * Side);
    cout << "The area of octagon is, "<< Area << endl; 
    }

    else
    {
    cout << "Unknown figure. Try again." << endl;    
    }

    }

Aucun commentaire:

Enregistrer un commentaire