Hello everybody I'm in the middle of programming a basic console application that is supposed to be a stock market game for my programming class in c++. I have run into this weird error involving a Visual Studio Just-In-Time Debugger window popping up saying I have a Win 32 unhandled exception even though I am using Code::Blocks 10.05 as my IDE, not Visual C++.
Attachment 10148
Now my whole project is rather long(at least from my point of view) so far but basically what happens is that I have a main function, a class named CStock(which is what the stocks in my game are made from) and then a bunch of other functions that make up different parts of the program (generating a random number, publishing the prices to the console, and some others).
I had started out declaring the different stocks after the Class using their names (Gold, Silver, Beef, etc), but then I wanted to declare them as an array instead so that it would be easier to add or change the amount of stocks I had in the game.
Once I changed to declaring the stocks after the class as an array, however, I got the error. I do not receive the error if I declare the array of stocks during a seperate function (not directly after the class), but then I cannot access the array of stocks in all the functions of my program.
Here Is my code so far and I would greatly appreciate any help/ advice anyone has to offer.
Code:
//This class creates a stock with the initial name and value inputted
class CStock
{
public:
int iPrice, iTrend, iChange, iAmountOwned;
string sStockName;
CStock(); //constructor
void ChangePrice()
//changes this stock's price
{
int iRanNum;
iRanNum = RandomNumber(-50, 50);
//iRanNum = (rand() % 101) - 50;
iPrice+=iRanNum;
iChange=iRanNum;
iTrend+=iRanNum;
};
void PublishPrice()
//publishes the current info for this stock
{
cout<<sStockName<<" Price: "<<iPrice<<" Change: "<<iChange<<" Trend: "<<iTrend<<endl;
};
void Buy(int iAmountToBuy)
//Buys an amount of stocks for the user
{
int iTotalCost;
//calculates total price
iTotalCost=iAmountToBuy*iPrice;
//only buys the stocks if you have enough money too
if(iUserMoney>=iTotalCost)
{
iUserMoney=iUserMoney-iTotalCost;
iAmountOwned+=iAmountToBuy;
cout<<endl<<"Money: "<<iUserMoney<<" "<<sStockName<<" Owned: "<<iAmountOwned<<endl;
}
else{cout<<endl<<"Get more money you hobo!"<<endl;};
};
void Sell(int iAmountToSell)
//Sells an amount of stocks for the user
{ //only sells if you input a valid amount to sell
if(iAmountOwned>0 && iAmountToSell<=iAmountOwned)
{
int iMoneyEarned;
//calculates how much money you will earn and adds it to user's total money
iMoneyEarned=iAmountToSell*iPrice;
//as well as subtracting the amount of stocks that were sold
iAmountOwned-=iAmountToSell;
iUserMoney+=iMoneyEarned;
cout<<endl<<"Money: "<<iUserMoney<<" "<<sStockName<<" Owned: "<<iAmountOwned<<endl;
}
else{cout<<endl<<"You cannot sell that many!"<<endl;} //outputs this message if an invalid amount is tried to be sold
}
void SetValues(string InpStockName, int InpPrice, int InpAmountOwned)
//this function sets the values for the stock
{
sStockName = InpStockName;
iPrice = InpPrice;
iAmountOwned = InpAmountOwned;
}
}Stock[4]; //this causes the error when declared here******************************************************************
//CGold("Gold", 1000), CSteel("Steel", 1000), CBeef("Beef", 1000), CWheat("Wheat", 1000), CSilver("Silver", 1000);
//Stock class constructor that sets the values to default before they are specified
CStock::CStock()
{
sStockName = "unnamed";
iPrice = 1000;
iTrend = 0;
iChange = 0;
iAmountOwned = 0;
};
void SetStockValues()
//this sets the values for all the declared stocks
{
//CStock Stock[4]; no error when declared here, but only usable within this function**************************************************
Stock[0].SetValues("Gold", 1000, 0);
Stock[1].SetValues("Silver", 1000, 0);
Stock[2].SetValues("Beef", 1000, 0);
Stock[3].SetValues("Wheat", 1000, 0);
Stock[4].SetValues("Steel", 1000, 0);
}
edited for linesize and readability. -nv