Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

CArray contains only the last added element

$
0
0

Hi this is my code - 

        CStdioFile objFileOpen;
	CFileException fileException;
	CString sLine;
	int iNumb,iIndex=0;
	iNumb=GetNoOfRows(sFile);

	CTechCall* objTechCall=new CTechCall[iNumb];
	CTechCall *ptr;

	ptr=objTechCall;

	if(objFileOpen.Open(sFile,CFile::modeRead, &fileException))
	{
		objFileOpen.ReadString(sLine); //Reads 1st line from csv file i.e. header

		while(iIndex<iNumb)
		{
			while(objFileOpen.ReadString(sLine)) //Reads each line from csv file
			{
				CString sToken=_T(",");
				int i = 0; // substring index to extract
				while (AfxExtractSubString(sToken, sLine, i,',')) //Splits the string at comma(,) and saves in sToken
				{   
				   switch(i+1)
				   {
					   case 1: ptr->setExchange(sToken);
						       iNitem = m_List.InsertItem(0, sToken,0);
						       break;
					   case 2: ptr->setSymbol(sToken);
							   m_List.SetItemText(iNitem,1,sToken);
						       break;
					   case 3: ptr->setAction(sToken);
						       m_List.SetItemText(iNitem,2,sToken);
						       break;
					   case 4: ptr->setCallTime(atol(sToken));
						       m_List.SetItemText(iNitem,3,sToken);
						       break;
					   case 6: ptr->setCallPrice(atof(sToken));
						       m_List.SetItemText(iNitem,4,sToken);
							   break;
					   case 7: ptr->setLivePrice(atof(sToken));
						       m_List.SetItemText(iNitem,5,sToken);
						       break;
					   case 8: ptr->setProfitTarget(atof(sToken));
						       m_List.SetItemText(iNitem,6,sToken);
							   break;
					   case 9: ptr->setExitPrice(atof(sToken));
						       m_List.SetItemText(iNitem,7,sToken);
							   break;
					   case 10: ptr->setExitTime(atol(sToken));
						        m_List.SetItemText(iNitem,8,sToken);
								break;			
				   }
				   i++;
				}
				techCallArray.Add(objTechCall[iIndex]);

				flag[iIndex]="Evaluating";
				m_List.SetItemText(iNitem,9,flag[iIndex]);

				ptr++;
			}
			iIndex++;
		}
	}else{
		CString strErrorMsg;
		strErrorMsg.Format(_T("Can't open file %s , error : %u"), sFile, fileException.m_cause);
		AfxMessageBox(strErrorMsg);
	}

when i try to print the techCallArray elements it prints only the last added element. For ex if there are 5 objects with symbol-HDFC, IGL, JUBLFOOD, IDBI, GMRINFRA respectively when i give

for(int j=0;j<iNumb;j++)

{

CTechCall a=techCallArray[j];

AfxMessageBox(a.getSymbol());

}

displays GMRINFRA 5 times. Can u tell me what is the mistake i have done?


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>