gpt4 book ai didi

c++ - 错误 : the program stops executing | Exit code: 255

转载 作者:搜寻专家 更新时间:2023-10-31 02:02:27 25 4
gpt4 key购买 nike

你能告诉我我哪里失败了吗?程序在执行时关闭。

之前,它可以完成执行,但没有显示我期望的结果。那是以前,现在它关闭并且没有显示结果。

例如:如果我需要discount的结果,它会在结果上显示“nan”,可能是有问题导致它没有完成执行。

现在,正如标题所说,它没有向我显示结果,而是显示退出代码:255。

但是,如果你去在线编译器,粘贴代码,执行它并按照说明操作,你会看到最后它不显示结果而是退出代码:139 Segmentation fault (core dumped)

我认为问题主要来自SalePrice(),也许我写错了,或者我缺少库,我还没有这方面的知识。

对不起,如果我没有很好地解释自己,我还在学习,这在我之前没有发生过。

感谢阅读到这里!

#include <iostream>
#include <sstream>
#include <math.h>

using namespace std;

class CarOnSale
{
private:
string Brand;
string Country;
float Model;
float ImportationCost;
public:
//CONSTRUCTOR

CarOnSale(string, string, float, float);

//DESTRUCTOR

~CarOnSale();

//SETTERS

void setBrand(string);
void setCountry(string);
void setModel(float);
void setImportationCost(float);

//GETTERS

string getBrand();
string getCountry();
float getModel();
float getImportationCost();

//ATTRIBUTES

float Antiquity();
float Discount();
float Comission();
float Taxes();
float CompanyProfitPercentage();
float SalePrice();
float TotalPrice();
string toString();

};


//CONSTRUCTOR CONSTRUCTOR CONSTRUCTOR

CarOnSale::CarOnSale(string pBrand, string pCountry, float pModel, float pImportationCost)
{
Brand=pBrand;
Country=pCountry;
Model=pModel;
ImportationCost=pImportationCost;
}
//DESTRUCTOR DESTRUCTOR DESTRUCTOR

CarOnSale::~CarOnSale()
{

}

//SETTER SETTER SETTER SETTER SETTER

void CarOnSale::setBrand(string pBrand)
{
Brand=pBrand;
}

void CarOnSale::setCountry(string pCountry)
{
Country=pCountry;
}

void CarOnSale::setModel(float pModel)
{
Model=pModel;
}

void CarOnSale::setImportationCost(float pImportationCost)
{
ImportationCost=pImportationCost;
}

//GETTER GETTER GETTER GETTER GETTER

string CarOnSale::getBrand()
{
return(Brand);
}

string CarOnSale::getCountry()
{
return(Country);
}

float CarOnSale::getModel()
{
return(Model);
}

float CarOnSale::getImportationCost()
{
return(ImportationCost);
}

//ATTRIBUTES ATTRIBUTES ATTRIBUTES ATTRIBUTES

float CarOnSale::Antiquity()
{
float ActualYear=2019;

return ActualYear-Model;
}

float CarOnSale::Discount()
{
float Discount=0;

if(Antiquity()>10)
{
return Discount=SalePrice()*0;
}
else
{
if(Antiquity()<10&&Antiquity()>5)
{
return Discount=SalePrice()*0.05;
}
else
{
if(Antiquity()<5)
{
return Discount=SalePrice()*0.015;
}
}
}
}

float CarOnSale::Comission()
{
float Comission=0;

if(SalePrice()>8000000||Country=="USA"||Country=="Germany")
{
return Comission=ImportationCost*0.12;
}
else
{
return Comission=ImportationCost*0.06;
}
}

float CarOnSale::Taxes()
{
float Taxes=0;

if(Country=="Germany")
{
return Taxes=SalePrice()*0.2;
}
else
{
if(Country=="Japan")
{
return Taxes=SalePrice()*0.3;
}
else
{
if(Country=="Italy")
{
return Taxes=SalePrice()*0.15;
}
else
{
if(Country=="USA")
{
return Taxes=SalePrice()*0.08;
}
}
}
}
}

float CarOnSale::CompanyProfitPercentage()
{
float CompanyProfitPercentage=0;

return CompanyProfitPercentage=ImportationCost*0.3;
}

float CarOnSale::SalePrice()
{
float SalePrice=0;

return SalePrice=ImportationCost+CompanyProfitPercentage()-Comission()-Discount()-Taxes();

}

float CarOnSale::TotalPrice()
{
float TotalPrice=0;

return TotalPrice=ImportationCost+CompanyProfitPercentage()-Comission()-Discount()-Taxes();

}

string CarOnSale::toString()
{
stringstream s;

s<<"Car is "<<Antiquity()<<" years old"<<endl;
s<<"Discount: "<<Discount()<<" percent"<<endl;
s<<"Car's comission: "<<Comission()<<endl;
s<<"Car taxes: "<<Taxes()<<endl;
s<<"Company Profit Percentage: "<<CompanyProfitPercentage()<<endl;
s<<"Price of the car: "<<SalePrice()<<endl;

return s.str();
}

int main()
{
//Variables
string BrandMAIN, CountryMAIN;
float ModelMAIN, ImportationCostMAIN;


//Object
CarOnSale Car(BrandMAIN, CountryMAIN, ModelMAIN, ImportationCostMAIN);


//Actions

cout<<"Write your car's brand: "<<endl;
cin>>BrandMAIN;

cout<<"Write your car's country: "<<endl;
cin>>CountryMAIN;

cout<<"Write your car's model (year): "<<endl;
cin>>ModelMAIN;

cout<<"write your car's importation cost: "<<endl;
cin>>ImportationCostMAIN;


//SETTERS

Car.setBrand(BrandMAIN);
Car.setCountry(CountryMAIN);
Car.setModel(ModelMAIN);
Car.setImportationCost(ImportationCostMAIN);


//Prints

cout<<Car.toString();



return 0;
}

最佳答案

您遇到了导致堆栈溢出的递归条件。 Commision() 调用 SalePrice(),后者依次调用 Commision(),等等。

重构您的函数,使它们不会递归地相互调用。

关于c++ - 错误 : the program stops executing | Exit code: 255,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com