Doubles Multiplication
Jan 27, 2016 at 1:25am UTC
So I am trying to get the perimeter of a regular polygon, but when I multiply the number of sides times the length of a side, its rounding. How do I keep it from rounding? I made both the side number and length doubles, and I tried using setprecision and fixed. What am I doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include "stdafx.h"
#include <iostream>
#include <string>
#include "p.h"
#include <iomanip>
using namespace std;
int main() {
double sides = 0.0;
double length = 0.0;
cout << "How many sides does the polygon have? " ;
cin >> sides;
cout << "What is the length of the sides? " ;
cin >> length;
Polygon pol;
pol.set_value(sides, length);
cout << "The perimeter of the polygon is: " << setprecision(5) << fixed << pol.polygonPerim() << endl;
system("pause" );
return 0;
}
#pragma once
#include <iostream>
#include <string>
#include "stdafx.h"
using namespace std;
class Polygon {
public :
//Setters and Getters
void set_value(double , double );
//Destructors
Polygon();
~Polygon();
//Perimeter
double polygonPerim();
private :
int _sides;
string name;
int _length;
};
#include "stdafx.h"
#include "p.h"
#include <iostream>
#include <string>
using namespace std;
//Constructors
Polygon::Polygon()
{
_sides = 0.0;
_length = 0.0;
}
//Destructors
Polygon::~Polygon()
{
}
//Setters and Getters
void Polygon::set_value(double sides, double length) {
_length = length;
_sides = sides;
}
//This will return the
//the perimeter of the
//polygon.
double Polygon::polygonPerim() {
return _sides * _length;
}
Jan 27, 2016 at 1:32am UTC
line 47 and 49: int.
a regular polygon: number of sides should be an int. Only the length needs to be double.
int * double will give a double as the result.
Last edited on Jan 27, 2016 at 1:36am UTC
Topic archived. No new replies allowed.