I need help with the code below. I keep receiving the error Run Time Check Failu
ID: 3568362 • Letter: I
Question
I need help with the code below. I keep receiving the error Run Time Check Failure # 2: stack around variable shares1 is corrupted. I don't know how to resolve this error and I need help ASAP on that specific portion of the code so that my code can run and end without an error popping up. Anyother discrepancies such as using strcopy instead of setting the char symbol to equal the char sym, please leave as is, Also need help setting up the prototypes for the functions in the classing and then define the actual function outside the class. Please and thank you, if the answer exceeds my expectations then I will try to find a way to give even more points!
#include "stdafx.h"
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Stock
{
char symbol[5];
double shareprice;
public:
Stock()
{
char symbol[5] = { 'null' };
shareprice = 0;
}
Stock(char sym[4], double price)
{
char symbol[5] = { sym[4] };
shareprice = price;
}
void setStock(char sym[4], double price)
{
symbol[5] = { sym[4] };
shareprice = price;
}
char* getSymbol()
{
return symbol;
}
double getSharePrice()
{
return shareprice;
}
};
class StockPurchase
{
Stock stockValue;
int shareToPurchase;
public:
StockPurchase()
{
stockValue = Stock();
shareToPurchase = 0;
}
StockPurchase(Stock sV, int purch)
{
stockValue = Stock(sV.getSymbol(), sV.getSharePrice());
shareToPurchase = purch;
}
void setSharesToPurchase(int purch)
{
shareToPurchase = purch;
}
double getCost()
{
return (stockValue.getSharePrice() * shareToPurchase);
}
};
int main()
{
char shares1[4];
char shares2[4];
char shares3[4];
double sharePr, pur;
std::cout << std::fixed;
Stock stock1 = Stock();
Stock stock2 = Stock();
Stock stock3 = Stock();
cout << "Enter 1st stock: ";
cout << "Enter symbol: ";
cin >> shares1;
cout << " Enter price: ";
cin >> sharePr;
stock1.setStock(shares1, sharePr);
cout << " Enter shares to purchase: ";
cin >> pur;
StockPurchase Sp1 = StockPurchase(stock1, pur);
cout << " 1st stock purchase of " << pur << " of " << shares1 << " costs: $" << std::setprecision(2) << Sp1.getCost() << " ";
cout << "Enter 2nd stock: ";
cout << "Enter symbol: ";
cin >> shares2;
cout << " Enter price: ";
cin >> sharePr;
stock2.setStock(shares2, sharePr);
cout << " Enter shares to purchase: ";
cin >> pur;
StockPurchase Sp2 = StockPurchase(stock2, pur);
cout << " 1st stock purchase of " << pur << " of " << shares2 << " costs: $" << std::setprecision(2) << Sp2.getCost() << " ";
cout << "Enter 3rd stock: ";
cout << "Enter symbol: ";
cin >> shares3;
cout << " Enter price: ";
cin >> sharePr;
stock3.setStock(shares3, sharePr);
cout << " Enter shares to purchase: ";
cin >> pur;
StockPurchase Sp3 = StockPurchase(stock3, pur);
cout << " 1st stock purchase of " << pur << " of " << shares3 << " costs: $" << std::setprecision(2) << Sp3.getCost() << " ";
cout << " ";
return 0;
}
Explanation / Answer
you just need to change the compiler.Use C++ 4.8.1 it's comiling correct for me.
I don't understand the other part of the question.if u need any further suggsetion plzz ask