为什么我的程序输出一个巨大的小数
Why does my program output a huge decimal?
好的,所以我创建了一个模拟景观公司的程序,所以我们必须计算草皮和围栏的成本。所以当我输入长度和宽度时,他们会输出巨大的小数,例如
Parkton Landscaping
Enter Length: 10
Enter width: 12
Lanscaping Costs
Sod = 6871947680.00
Fence = 19327352760.00
Press any key to continue . . .
假定Sod=56.40假设Fence=990.00
请帮忙,这是我的代码和两个文件
#include <iostream>
#include <iomanip>
using namespace std;
#include "c:UsersbartaOneDriveDocumentsVisual Studio 2015ProjectsProject 6Project 6Geometry.h"
#include "Pricing.h"
int main()
{
int length, width;
Pricing landscape;
Geometry geo;
const double Fenceprice = 22.50;
const double Sodprice = .47;
cout << "t Parkton Landscaping " << endl;
cout << "Enter Length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
//Pricing(length, width);
//geo.getLength();
//geo.getWidth();
std::cout << std::fixed << std::setprecision(2);
landscape.displayOutput();
cout << "Sod = " << landscape.getsodCost(length) << endl;
cout << "Fence = " << landscape.getFenceCost(Fenceprice) << endl;
system("pause");
return 0;
}
这是头文件:
#pragma once
class Geometry
{//an object is how you access the class
public:// where the public function definitions are created
Geometry();// Default Constructor
Geometry(int, int);
Geometry(int);
void setLength(int); //assigns length
void setWidth(int); //assigns width
void setSide(int); //assigns side
//Constructor function that recieves the values for the rectangle
//Constructor function that recieves values for the cube
int getLength(),
getWidth(),
getSide(),
getArea(),
getPerimeter(),
getSurfaceArea();
private: //where the private members are created
int length,
width,
side;
void checkNum(int); //function that checks to see if the number is less than 0
};
Geometry::Geometry()
{
length = length;
width = width;
side = 0;
}
Geometry:: Geometry(int length, int width) /*function recieves 2 intergers and calls checkNum to validate if */
{
setLength(length);
setWidth(width);
checkNum(length);
checkNum(width);
}
Geometry:: Geometry(int sides)
{
checkNum(sides);
setSide(sides);
}
int Geometry::getLength()
{
return length;
}
int Geometry::getWidth()
{
return width;
}
int Geometry::getSide()
{
return side;
}
int Geometry::getArea()
{
return length * width;
}
int Geometry::getPerimeter()
{
return 2 * (length + width);
}
int Geometry::getSurfaceArea()
{
return 6 * (side * side);
}
void Geometry::setLength(int len)
{
length = len;
checkNum(len);
}
void Geometry::setWidth(int widths)
{
width = widths;
checkNum(widths);
}
void Geometry::setSide(int s)
{
side = s;
checkNum(s);
}
void Geometry::checkNum(int num) //function checks to see if the number is less than zero
{
if (num <= 0)
{
cout << "!!!!!!!!WARNING!!!!!! this isnt a number" << " program will now exit......" << endl;
system("pause");
exit(1);
}
}
头文件#2
#include "Geometry.h"
class Pricing : Geometry
{
public:
Pricing();
Pricing(int length, int width);
double getsodCost(double);
double getFenceCost(double);
void displayOutput();
private:
};
Pricing::Pricing(int length, int width) :Geometry(length, width)
{
}
Pricing::Pricing()
{
}
double Pricing::getsodCost(double price)
{
getArea();
return getArea()*price;
}
double Pricing::getFenceCost(double price)
{
getPerimeter();
return getPerimeter()*price;
}
void Pricing::displayOutput()
{
cout << "nn";
cout << "t Lanscaping Costs " << endl;
}
因为您从不使用有效值初始化对象,这意味着Geometry::width
和Geogrpapy::length
未初始化,并且具有不确定的值。在未初始化的情况下使用它们会导致未定义的行为。
相关文章:
- 将数字提高到一个巨大的指数
- 使用C++对一个巨大文件中的文本行进行字典式外部排序
- 如何在一个巨大的文本文件中对整数进行排序
- 在 Ubuntu(linux OS)中将一个巨大的文件读入C++向量
- 在C 中创建一个巨大的标志地图
- 我的函数总是返回一个巨大的数字
- 迭代一个巨大的 std::vector of std::vectors 并修改元素
- g++:编译一个巨大的静态std::vector
- 为什么我的程序输出一个巨大的小数
- C++程序在访问一个巨大的数组时会冻结
- 如何将一个巨大的二维数组初始化为类中的空间
- std::queue::size() 可以在 size() == 0 的 pop() 之后返回一个巨大的数字
- 使用C++解析一个巨大而复杂的CSV文件
- K的意思是有一个巨大的阵列
- 为什么这个int会随机变成一个巨大的负值
- 如何在Objective-C中包装一个巨大的静态c++库
- 在Java中使用SWIG,我如何有选择地SWIG一个巨大的C/ c++头文件的某些部分
- 内存映射一个巨大的文件在32位软件运行在64位操作系统
- 在c++中将一个巨大的文本文件(2Gb以上)分成2个块
- 计算pow(double,2)得到一个巨大的数字(C)