[英]Error: the program stops executing | Exit code: 255
你能告訴我我哪里失敗了嗎? 程序在執行時關閉。
之前,它可以完成執行,但它沒有顯示我期望的結果。 那是以前,現在它關閉並且沒有向我顯示結果。
例如:如果我需要折扣的結果,它會在結果上顯示一個“ 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()
,而SalePrice()
又不斷調用Commision()
等。
重構您的函數,使它們不會相互遞歸調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.