For some reason the variable "percent" is returning 0 when divided by mArea

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
int sum, nShapes, mLength, mWidth, mArea, percent;
mLength = 0;
mWidth = 0;
sum = 0;
cout << "How many shapes do you want to calculate the area for? ";
cin >> nShapes;;
for (int i = 1; i <= nShapes; i = i + 1)
{
int x1, y1, x2, y2, x3, y3, x4, y4, area;
string object;
cout << "Enter the coordinates: ";
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
if (abs(x1 - x3) == abs(y1 - y3)) {
object = "Square";
}
else {
object = "Rectangle";
}
area = abs(x1 - x3) * abs(y1 - y3);

cout << "Shape #:" << setw(13) << "Type:" << setw(30) << "Area (sq/units):" << endl;
cout << i << setw(20) << object << setw(20) << area << endl;
sum = sum + area;
int array[4] = { x1, x2, x3, x4 };
for (int j = 1; j <= 4; j = j + 1)
{
if (array[j] > mLength)
mLength = array[j];
}
int array1[4] = { y1,y2,y3,y4 };
for (int k = 1; k <= 4; k = k + 1)
{
if (array1[k] > mWidth)
mWidth = array1[k];
}
}
mArea = mLength * mWidth;
percent = (sum / mArea) * 100;
cout << "The total are of the shape is: " << sum << " sq/units" << endl;
cout << "Minimum Material Specifications: " << endl;
cout << "Length: " << mLength << "\t\t" << "Width: " << mWidth << "\t\t" << "Area: " << mArea << " sq/units" << endl;
cout << "The layout uses " << fixed << setprecision(2) << percent << "% of the material" << endl;
}
Last edited on
mArea is getting the right value and so is sum, but it does not work when they are divided.
Avoid integer division
1
2
// percent = (sum / mArea) * 100;
percent = ( double(sum) / mArea ) * 100;


You may want to consider using double as the type of the variable percent
Oh, i completely blanked on that. Thank you.
Topic archived. No new replies allowed.