Вылетает предположительно из-за вот этих строк:
bAll = new Buy[n];
bAll[n].SetCost(Cost);
bAll[n].SetName(Name);
Сам код программы:
#include
#include
using namespace std;
class Buy
{
private:
int* iCost;
string* sName;
public:
Buy()
{
iCost = new int;
*iCost = 0;
sName = new string;
*sName = "Help me";
}
int GetCost()
{
return *iCost;
}
string GetName()
{
return *sName;
}
void SetCost(int Cost)
{
*iCost = Cost;
}
void SetName(string Name)
{
*sName = Name;
}
};
int main()
{
cout << "Type help for help\n";
int n = 1;
int Cost = 0;
string Talk;
string Name = "Fix Me";
Buy* bAll;
while (true)
{
cin >> Talk;
if (Talk == "help")
{
cout << "create - new product\n";
cout << "show - get product\n";
}
else if (Talk == "create")
{
cout << "Cost: ";
cin >> Cost;
cout << "Name: ";
cin >> Name;
bAll = new Buy[n];
bAll[n].SetCost(Cost);
bAll[n].SetName(Name);
}
else if (Talk == "show")
{
cout << "Cost of " << bAll[n].GetName() << " is " << bAll[n].GetCost() << '\n';
}
}
}
bAll = new Buy[n];
bAll[n].SetCost(Cost);
bAll[n].SetName(Name);
Сам код программы:
#include
#include
using namespace std;
class Buy
{
private:
int* iCost;
string* sName;
public:
Buy()
{
iCost = new int;
*iCost = 0;
sName = new string;
*sName = "Help me";
}
int GetCost()
{
return *iCost;
}
string GetName()
{
return *sName;
}
void SetCost(int Cost)
{
*iCost = Cost;
}
void SetName(string Name)
{
*sName = Name;
}
};
int main()
{
cout << "Type help for help\n";
int n = 1;
int Cost = 0;
string Talk;
string Name = "Fix Me";
Buy* bAll;
while (true)
{
cin >> Talk;
if (Talk == "help")
{
cout << "create - new product\n";
cout << "show - get product\n";
}
else if (Talk == "create")
{
cout << "Cost: ";
cin >> Cost;
cout << "Name: ";
cin >> Name;
bAll = new Buy[n];
bAll[n].SetCost(Cost);
bAll[n].SetName(Name);
}
else if (Talk == "show")
{
cout << "Cost of " << bAll[n].GetName() << " is " << bAll[n].GetCost() << '\n';
}
}
}