Need urgent help compile issue

Pages: 12
could this be the reason for test failure?


Yep - as all calculations in my code above use type double. You'll need to change the types from double to int. Note this isn't particularly good practice - especially for the area of a circle!
Ok, understood. So how can i fix this issue then? please suggest if changing data types would resolve the issue here.
I changed all datatypes from double to int and tested code.
Below is the output. Compiler output is 95, eventhough expected output must be 96. I am not sure what went wrong here.

Input (stdin)

4 3 5 2 5
Your Output (stdout)
Enter rect height, rect width, tria height, tria width, circ rad: 95

Expected Output
96
Like this, as this is what is expected - but not recommended!

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
#include <iostream>
#include <vector>
#include <memory>
#include <numeric>
#include <cmath>
//#include <string>

class Shape {
protected:
	//std::string name;
	int width {}, height {}, radius {};

public:
	Shape(int w, int h) : width(w), height(h) {}
	Shape(int r) : radius(r) {}
	virtual ~Shape() {}

	void set_data(int a, int b) {
		width = a;
		height = b;
	}

	virtual int getarea() const = 0;
};

class Rectangle : public Shape {
public:
	Rectangle(int w, int h) : Shape(w, h) {}

	int getarea() const override { return width * height; }
};

class Triangle : public Shape {
public:
	Triangle(int w, int h) : Shape(w, h) {}

	int getarea() const override { return (width * height) / 2; }
};

class Circle : public Shape {
public:
	Circle(int r) : Shape(r) {}

	int getarea() const override { return std::round(3.1415 * (radius * radius)); }
};

int main()
{
	int rectHeight {}, rectWidth {};
	int triaHeight {}, triaWidth {};
	int circRadius {};

	std::cout << "Enter rect height, rect width, tri height, tri width, circ rad: ";

	std::cin >> rectHeight >> rectWidth >> triaHeight >> triaWidth >> circRadius;

	std::vector<std::unique_ptr<Shape>> shapes;

	shapes.emplace_back(std::make_unique<Rectangle>(rectHeight, rectWidth));
	shapes.emplace_back(std::make_unique<Triangle>(triaHeight, triaWidth));
	shapes.emplace_back(std::make_unique<Circle>(circRadius));

	const auto totalArea {std::accumulate(shapes.begin(), shapes.end(), 0, [](auto total, const auto& shape)
			{ return total + shape->getarea(); })};

	std::cout << totalArea << '\n';
}



Enter rect height, rect width, tri height, tri width, circ rad: 4 3 5 2 5
96

Thanks a lot seeplus, for your quick suggestions and guidance.
Topic archived. No new replies allowed.
Pages: 12