9 Replies - 259 Views - Last Post: 18 December 2012 - 01:37 AM Rate Topic: -----

#1 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Maps and iterators incorrect assignment

Posted 17 December 2012 - 08:26 PM

Hi, I am trying to code a stock simulator program using maps and iterators. I am new to the topic. My specific challenge is having the program store the values correctly or this is what I assume the problem is.

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Maps and iterators incorrect assignment

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Maps and iterators incorrect assignment

Posted 17 December 2012 - 09:07 PM

A function to enter stocks would be good.


bool enter_stocks(map<string, double> &ticker);




To check whether a stock is in the map,count is quite useful, because if the stock is not in the map a zero will be returned.
Was This Post Helpful? 0
  • +
  • -

#3 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Maps and iterators incorrect assignment

Posted 17 December 2012 - 10:33 PM

Yes I agree having a function to enter stocks would be good, however I am still looking to find what is wrong with my logic. All my open_ticker values are being replaced. Can you help with the error in my logic?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Maps and iterators incorrect assignment

Posted 17 December 2012 - 11:16 PM

It is the way maps work. A stock will be present or not. If not present the stock will be added. If it is present it will be updated.


// create variable for difference ?

if stock not present
  // add to maps
   set close_ticker = quote
   set open_ticker = quote
else
  // stock present
  // update maps
   set close_ticker = open_ticker
   set open_ticker = quote
endif

  // calculate difference
   difference could be open_ticker - close_ticker





What is slightly confusing is that a closing price is usually later than an opening price. You seem to be saying that a closing price is the previous price and opening price is the current price. If that is correct previous and current would be clearer.
Was This Post Helpful? 0
  • +
  • -

#5 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Maps and iterators incorrect assignment

Posted 17 December 2012 - 11:37 PM

No that is correct. I want to keep the open prices static, however then close prices (probably not a good choice for variable name) should change based on the continuous flow of prices and the output should indicate by how much the last price changed either up or down.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Maps and iterators incorrect assignment

Posted 17 December 2012 - 11:59 PM

I thought maybe you could have a map for differences.

Then I thought a map of struct/class would be better.


struct StockPrice  {
  double open;
  double last;
  double diff;
};

int main()
{

  map<string, StockPrice> ticker;

}





if stock not present

  StockPrice temp;
  temp.open = quote;
  temp.last = quote;
  temp.diff = 0;
  // add to maps
  ticker[stock] = temp;

else
  // stock present
  // update a temp
  // update maps
endif


Was This Post Helpful? 1
  • +
  • -

#7 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Maps and iterators incorrect assignment

Posted 18 December 2012 - 12:43 AM

It's truly beautiful. I would have never thought of something so simple yet extravagant.

Just out of curiosity, so I can become a better coder, do you know what was wrong with the logic in my code?
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Maps and iterators incorrect assignment

Posted 18 December 2012 - 01:20 AM

The main problem seems to be as you say, you don't know whether you are updating the map or inserting a new value. That is a problem because you want the last value (close price) to calculate the difference.

Another way to do this is to use insert that will only change the map if the stock doesn't exist.


  // instead of 
  //open_ticker[stockSymbol] = quote;
  //close_ticker[stockSymbol] = quote;

  // use insert
  open_ticker.insert ( pair<string, double>(stockSymbol, quote) );
  close_ticker.insert ( pair<string, double>(stockSymbol, quote) );

  // now we know that the stock is in the map
  // but no data has been otherwise updated

  // calculate the difference
  diff = quote - close_ticker[stockSymbol];

  // now the close_ticker map can be updated




I think something like that should work
Was This Post Helpful? 1
  • +
  • -

#9 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Maps and iterators incorrect assignment

Posted 18 December 2012 - 01:24 AM

I will probably have to recode it and then post it up, since we are using pair, which I think is another class on its own. Thanks.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Maps and iterators incorrect assignment

Posted 18 December 2012 - 01:37 AM

Yes, using the count function doesn't need pair.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1