-1

I wrote a simple code to calculate the perimeter of a polygon. Code shown below.

#include<iostream>
 
using namespace std;

int main(){

    string polygon;
    int ns; //ns is number of sides
    float ls,perimeter; //ls length of sides 
    cout<<"Program to calculate the perimeter of a regular polygon.";
    cout<<"\n********************************************************";
    cout<<"\n\nEnter the name of the polygon: ";
    cin>>polygon;
    cout<<"\nEnter the number of sides: ";
    cin>>ns;
    cout<<"\nEnter the lenth of the side in millimeters: ";
    cin>>ls;    
    //perimeter calculation
    perimeter=ns*ls;
    cout<<"\nThe perimeter of a " <<polygon<< " with length " <<ls<< "millimeters is "<<perimeter;

    return 0;
}

Now instead of asking the user to enter the number of side of the polygon, how can let the program know the number of sides and also be able to calculate the perimeter? Yes I want the program to know the sides that corresponds to each polygon. Say, Enter polygon, and the user enters pentagon. I want the program to already know the side of the pentagon and also be able to calculate the perimeter when the length is entered. I'm pretty sure there is going to be IF and THEN "thing" involved but as a noob all those are just gibberish to me at the moment.

  • 4
    Take a piece of paper and write down the exact series of steps you would say to do this if you were explaining it to a person who had never seen polygons before and who would take everything you say very literally. Afterwards, look at how to translate those steps into your program. – TheUndeadFish Sep 06 '21 at 21:00
  • Do you no longer want to receive the number of sides from the user? You could just initialise your ns variable with the value you want if that's the case – Matt Sep 06 '21 at 21:15
  • Yes I want the program to know the sides that corresponds to each polygon. Say, Enter polygon, and the user enters pentagon. I want the program to already know the side of the pentagon and also be able to calculate the perimeter when the length is entered. – krankenhaus Sep 06 '21 at 21:24
  • @TheUndeadFish already told you how to do it. First you need to understand yourself the relationship between the name of the polygon and the number of sides. Only then you can figure out how to explain this to the computer. – Useless Sep 06 '21 at 21:43

1 Answers1

1

It looks like you want to parse language (i.e. hexagon -> 6, pentagon -> 5). For this you could try removing the last 3 letters and parse the meaning of the rest (hexa -> 6, penta -> 5, octo -> 8), but language processing is hard, especially if you don't understand what an if else is, and is further complicated by triangles, squares and other polygons not following the standard n-gon formula.

But how would you parse it? If you don't know the basic if-else, switch-case or other flow control statements, this will not be very useful for you. I recommend you learn a bit more then before you come back to this. If you do know these structures then I implore you to look at this. It includes basically all the standard polygon names and is where I would start. If you don't want to extend your program to count in greek (? I think) to have 22-gon in a greek way, then you should be fine with having a lookup table for those that are special and parsing the rest as having the name n-gon.

You can generate a lookup table using std::unordered_map<std::string, int> like:

const std::unordered_map<std::string, int> names = {
{"triangle",3},
{"square",4},
{"pentagon",5},
...
};
std::string in;
std::cin >> in;
int sides = names.at(in);

To decide when to parse and when to lookup you can use many ways to find if your map contains an element like std::unordered_map<KeyType,ValueType>::contains(KeyType) in c++20 or this before. Using these you should be able to achieve what you need. I deliberately did not include a working example of the code, but what is contained here should be more than enough to construct a working program.

Also don't make a habit of using using namespace std;.

Lala5th
  • 1,137
  • 7
  • 18