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.