C++ 为什么我的程序输出一个巨大的十进制数?

C++ 为什么我的程序输出一个巨大的十进制数?,c++,visual-studio,inheritance,project,C++,Visual Studio,Inheritance,Project,好的,我创建了一个程序,模拟了一家园林公司,所以我们必须计算草皮和围栏的成本。因此,当我输入长度和宽度时,他们会输出巨大的小数 Parkton Landscaping Enter Length: 10 Enter width: 12 Lanscaping Costs Sod = 6871947680.00 Fence = 19327352760.00 Press any key to continue . . . 假设Sod=56.4

好的,我创建了一个程序,模拟了一家园林公司,所以我们必须计算草皮和围栏的成本。因此,当我输入长度和宽度时,他们会输出巨大的小数

          Parkton Landscaping
Enter Length:  10
Enter width:   12


         Lanscaping Costs
Sod    =  6871947680.00
Fence  =  19327352760.00
Press any key to continue . . .
假设Sod=56.40 假设围栏=990.00

请帮助这里是我的代码和两个文件

#include <iostream>
#include <iomanip>

using namespace std;

#include "c:\Users\barta\OneDrive\Documents\Visual Studio 2015\Projects\Project 6\Project 6\Geometry.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;
}
#包括
#包括
使用名称空间std;
#包括“c:\Users\barta\OneDrive\Documents\Visual Studio 2015\Project\Project 6\Project 6\Geometry.h”
#包括“Pricing.h”
int main()
{
int长度、宽度;
定价格局;
几何地理;
const-double-Fenceprice=22.50;
const-double-Sodprice=.47;
库特宽度;
//定价(长度、宽度);
//geo.getLength();
//geo.getWidth();

std::cout因为您从不使用有效值初始化对象,这意味着
Geometry::width
Geogrpapy::length
未初始化且具有不确定的值。使用未初始化的对象会导致未定义的行为。

那么我会在几何体头文件中初始化这些对象吗?@BartAlen您可能应该是c调用
getArea
之前,先调用
setLength
setWidth
函数。我试过了,但仍然得到了相同的结果@Joachim pileborge我想Joachim已经找到了根本原因。你介意显示你初始化该类的代码吗?@BartWell我试过了,它说该函数无法访问
#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);
    } 

}
#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 << "\n\n";
    cout << "\t Lanscaping Costs " << endl; 


}