Click here to Skip to main content
15,886,829 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all,

Can someone please help or direct me to an example i can study.

I am trying to total columns in a list control which has 30 columns.

My problem is depending on each user the column count can change from 5 to 7 or 9 or more.

I am using 5 as an example:
if i am at column 0: I would like to have totals of column 0,1,2,28,29.
if I am at column 29: I would like to have total of column 29,0,1,27,28.

Currently I have the above in a for loop with an if statement hard coded with 5 column total. Is there a way, if i allow user to change from 5 to 7, I could dynamically get the total instead of it being hard coded.

Any help or pointing in right direction might help.

I have part of the code as to what I am currently doing.
for (int a=0; a<12;a++)
	{
		CString Temp;
		if (a==0)
		{
			Temp = m_Test.GetItemText(0,0);
			Temp = m_Test.GetItemText(0,11);
			Temp = m_Test.GetItemText(0,10);
			Temp = m_Test.GetItemText(0,1);
			Temp = m_Test.GetItemText(0,2);
			
			return;
		}
		if (a==1)
		{
			Temp = m_Test.GetItemText(0,1);
			Temp = m_Test.GetItemText(0,11);
			Temp = m_Test.GetItemText(0,0);
			Temp = m_Test.GetItemText(0,3);
			Temp = m_Test.GetItemText(0,2);

			return;
		}
		if (a==10)
		{
			Temp = m_Test.GetItemText(0,10);
			Temp = m_Test.GetItemText(0,11);
			Temp = m_Test.GetItemText(0,0);
			Temp = m_Test.GetItemText(0,9);
			Temp = m_Test.GetItemText(0,8);

			return;
		}
		if (a==11)
		{
		
			Temp = m_Test.GetItemText(0,11);
			Temp = m_Test.GetItemText(0,0);
			Temp = m_Test.GetItemText(0,1);
			Temp = m_Test.GetItemText(0,9);
			Temp = m_Test.GetItemText(0,10);

			return;
		}
		Temp = m_Test.GetItemText(0,a-1);
		Temp = m_Test.GetItemText(0,a-2);
		Temp = m_Test.GetItemText(0,a);
		Temp = m_Test.GetItemText(0,a+1);
		Temp = m_Test.GetItemText(0,a+2);

	}
Posted
Comments
H.Brydon 20-Sep-13 0:08am    
Your question is not clear and your code is nonsense. You are not doing any totalling (ie. accumulation) and most code optimizers would just throw all your code away and return to the caller.
FISH786 20-Sep-13 3:08am    
I am sorry. I must have not mentioned in my question. I haven't pasted the part I do the totaling. The totaling is easy.

1 solution

What you appear to be missing is a method to wrap the index around at the last column. Then '%' (remainder) operator will give you a tool for that. For example:
C++
const int numOfColumns = 30;
int idx = 28;
int total = 0;
for (int k = 0; k < 5; ++k)
  total += columns[(idx + k) % numOfColumns];

The other thing you seem to need is a way of converting the string you read from a cell into a number:
C++
CString s;
s = m_Test.GetItemText(0,11);
int x = atoi (s);

will do that for you. Combine these two things and you are done.
 
Share this answer
 
Comments
FISH786 20-Sep-13 3:11am    
Thank you.
nv3 20-Sep-13 3:13am    
Welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900