0

This is my first C++ programming practice. When I run 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;
        }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Unknown Shin
  • 99
  • 2
  • 10
  • You should learn [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/): stepping through the code and inspecting variable values would tip you off. Hint: what's the value of `equaliteral_triangle` and the other variables defined on the same line? – Angew is no longer proud of SO Feb 01 '18 at 17:43
  • You might also be interested in a [good C++ book](https://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Feb 01 '18 at 17:43
  • you have not set your equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon variables initial values – Maddy Feb 01 '18 at 17:43

3 Answers3

1

char equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon; You have declared these variables and haven't initialized them IT should be char equaliteral_triangle='t', square='s', pentagon='p', hexagon='h', heptagon='H', octagon='o';

0

You need to give some values to the char variables you defined. Without giving them some values they will remain undefined and therefore you will not match them. Note that these

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

are variables. If you type square into the console, it will not match the square variable's value. So, first of all, work with the characters like this:

    char figure;
    char equaliteral_triangle = '3',square = '4',pentagon = '5',hexagon = '6',heptagon = '7',octagon = '8';

and when you are successful and the program works, refactor it so that it will use a switch-case instead of if-else if which is more elegant in this case, when you have characters.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
-1

You have no values in your chars which means they all are NULL and when you compare them with the value user entered they will not be equal.

I would recommend only using the number of sides only if you want to have only these figures.

Eg if you have 3 sides you know it would be a triangle so use the formula accordingly .

theboringdeveloper
  • 1,429
  • 13
  • 17