Albert F D
Albert F D

Reputation: 529

C++ - Simple shape area calculator returns erroneous calculations

#include <iostream>
#include <iomanip>
using namespace std;

class Rectangle 
{
float x, y;
public:
void value (float,float);
float area () {return (x*y);}
};

void Rectangle::value (float a,float b) 
{
x = a;
y = b;
}

class Circle 
{
float x;
public:
void value (float);
float area () {return (3.14*x*x);}
};

void Circle::value (float a) 
{
x = a;
}

int main () 
{
float q,a,b;
char reply;

cout << "\t\tArea Calculator";
do
{
cout << "\n\nPlease select from the following: ";
cout << "\n1. Rectangle";
cout << "\n2. Cirlce";
cout << "\n3. Exit";
cout << "\n\n";
cin >> q;
if (q==3)
break;

if (q==1)
{
system("cls");
Rectangle rect;
cout << "\nPlease enter length: ";
cin >> a;
cout << "\nPlease enter width: ";
cin >> b;
cout << "\nArea: " << rect.area();
cin.get();
cout << "\n\nDo you want to continue y/n: ";
cin >> reply;

    if (toupper(reply) == 'N') 
        {
        cout << "\n\n";
        cout << "Goodbye!";
        break;
        }
}

if (q==2)
{
system("cls");
Circle circ;
cout << "\nPlease enter radius: ";
cin >> a;
cout << "\nArea: " << circ.area();
cin.get();
cout << "\n\nDo you want to continue y/n: ";
cin >> reply;

    if (toupper(reply) == 'N') 
        {
        cout << "\n\n";
        cout << "Goodbye!";
        break;
        }
} 

}   while (toupper(reply!='Y'));
{
cout << "\n\n";
system("pause");
}
}

The code above, debugs with the following warning:

"warning C4244: 'return' : conversion from double to float, possible loss of data"

... I am pretty sure this is the reason for the mis-calculations when the code is run (for e.g. it returns the area of a 5x5 square as 1.15292e+016) - Please will anyone explain the correct method for resolving this, I can't seem to get my rather-dopey-head around it :(

Upvotes: 0

Views: 385

Answers (2)

Avi Perel
Avi Perel

Reputation: 422

The wrong result is not a result of double to float conversion but rather a result of not initializing the dimensions members of the Rectangle/Circle objects. You need to call rect.value(a,b) after reading the values from the user. Not doing this leaves the object's x and y members uninitialized, therefore containing some arbitrary values - leading to wrong result of the calculation. Same goes for calling circ.value(a) for the circle.

cout << "\nPlease enter length: ";
cin >> a;
cout << "\nPlease enter width: ";
cin >> b;
rect.value(a,b);
cout << "\nArea: " << rect.area();

The warning on double to float conversion is probably the result of the "cin >> b" style lines. The cin stream >> operator handles reading double, not floats. When you attempt to read from cin into a float, you first get a double value which is then implicitly cast to float. As float has lesser accuracy than double, the compiler warns you that you may lose precision doing this. Assuming float accuracy is enough - this poses no problem. You can solve this simply by declaring the classes and variable to use double instead of float.

Upvotes: 1

P0W
P0W

Reputation: 47794

You're not initializing the data members of the class

rect.area(); and circ.area(); are calculating area on some garbage values

Use constructors:

Rectangle(float a, float b):x(a),y(b){}

Circle(float a):x(a){}

Then

cin >> a;
cin >> b;
Rectangle rect(a,b);
rect.area();

and

cin >> a;
Circle circ(a);
circ.area();

Upvotes: 0

Related Questions