#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");
}
}
上面的代码调试时出现以下警告:
“警告 C4244:'return':从 double 转换为 float,可能丢失数据”
...我很确定这是代码运行时计算错误的原因(例如,它返回 5x5 正方形的面积为 1.15292e+016) - 请任何人解释解决此问题的正确方法,我似乎无法理解它:(