For example, when you enter a stock say XYZ and a price for the first time it should be stored in a map. Then when you enter a stock with a name you have entered before it should look at the previous close price and calculate the difference between the prices.
My program seems to be replacing the first value rather than replacing the second value and spitting out the change between the second or the close price. Any help would be greatly appreciated.
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, double> open_ticker, close_ticker;
map<string, double>::iterator openiter;
map<string, double>::iterator closeiter;
string stockSymbol;
double quote;
string menu = "Please enter 1 to enter stocks, or enter 2 to quit program: ";
bool run = true;
while(run)
{
cout << menu;
int option;
cin>>option;
switch(option)
{
case 1:
cout<<"Please enter the stock symbol:";
cin>>stockSymbol;
cout<<"Please enter stock price:";
cin>>quote;
open_ticker[stockSymbol] = quote;
close_ticker[stockSymbol] = quote;
openiter = open_ticker.find(stockSymbol);
closeiter = close_ticker.find(stockSymbol);
double change;
double ticker;
double otherprice;
ticker = closeiter->second;
cout<<"ticker is "<< ticker <<endl;
close_ticker.erase(stockSymbol);
close_ticker[stockSymbol] = quote;
closeiter = close_ticker.find(stockSymbol);
otherprice = closeiter->second;
cout<<"otherprice is "<< otherprice <<endl;
change = otherprice - ticker;
cout<<"change is "<< change <<endl;
for (openiter = open_ticker.begin(); openiter != open_ticker.end(); openiter++)
{
cout << openiter->first << " => " << openiter->second << endl;
}//end for
//for (closeiter = close_ticker.begin(); closeiter != close_ticker.end(); closeiter++)
//{
// cout << closeiter->first << " => " << closeiter->second << endl;
//}//end for
break;
case 2:
run = false;
break;
}//end switch
}//end while
system("Pause");
return 0;
}

New Topic/Question
Reply




MultiQuote




|