Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
What could be wrong with this? It was working fine and then...Improper Argument.

Could it have something to do with deleting records from the array?

C++
for(int i = 0; i < FirstName.GetCount(); i++)
    {
        nItem = m_ListCtrl.InsertItem(0, FirstName[i] + L" " + LastName[i]);
        for(int j = 0; j < 8; j++)
        {
            if(Item[numItems].IsEmpty() == FALSE)
            {
            nItem = m_ListCtrl.InsertItem(1, L"");
            m_ListCtrl.SetItemText(nItem, 1, Item[numItems]);
            m_ListCtrl.SetItemText(nItem, 2, Quantity[numItems]);
            m_ListCtrl.SetItemText(nItem, 3, Cost[numItems]);
            m_ListCtrl.SetItemText(nItem, 4, Price[numItems]);
            m_ListCtrl.SetItemText(nItem, 5, Profits[numItems]);
            }
            numItems++;
        }
    }
Posted
Comments
Niklas L 17-Aug-12 5:04am    
Should you be using 'numItems'? Looks like you should be using 'i' to index your arrays (or whatever they are)
Are you sure the number of items in FirstName equals the number of items in your other collections?
JackDingler 17-Aug-12 11:14am    
That and the code has two InsertItem calls.

That might be the correct behavior, but it looks unusual.
DrBones69 17-Aug-12 20:03pm    
I'm populating my list control with the customer name and then there can be up to 8 items per customer. I list the customer and then the items associated with that customer, this is why I use "numItems" instead of "i". "i" is used to index the names and "numItems" is used to index the items per customer.

Your best friend is as in most cases your debugger. But from just looking at the code I would suspect that
nItem = m_ListCtrl.InsertItem(0, FirstName[i] + L" " + LastName[i]);

might return -1 when not successful (for whatever reason) and the following

m_ListCtrl.SetItemText(nItem, 1, Item[numItems]);


will fail because nItem is -1.
 
Share this answer
 
Comments
DrBones69 17-Aug-12 20:05pm    
This code was working until I deleted a few customer records.
I was trying to read my arrays beyond the amount of data that the arrays had stored. This is what I did to fix this issue:

C++
s7 = (((_wtof(myData[nPos].m_sPrice7) / _wtof(myData[nPos].m_sQty7)) - _wtof(myData[nPos].m_sCost7)) * _wtof(myData[nPos].m_sQty7));
            t7.Format(L"%.2f", s7);
            if(s7 > 0)
                dlg.Profits.Add(t7);
            else
                dlg.Profits.Add(L"0");


I added the else statement to fix my problem, it works fine again.
 
Share this answer
 
Comments
Richard MacCutchan 18-Aug-12 4:50am    
I have seen some bad code in my time but ...

You have concatenated a lot of functions each of which may fail and produce a totally incorrect result, without any checking on where or how.
DrBones69 18-Aug-12 23:41pm    
Is there a better way to extract CString objects and convert them to integers? Please elaborate on your comment.
Richard MacCutchan 19-Aug-12 4:27am    
There is nothing inherently wrong with using _wtof(), but you have four calls to it in one expression, any of which could fail. You should check the result of each call to ensure that you are dealing with valid data rather than just assuming that it is all correct. A small amount of checking at this point will pay dividends in the future when debugging other problems.

DrBones69 19-Aug-12 17:14pm    
What is a suitable practice for checking the results of the _wtof() function? What do I check for? Should I just check for -1? Please enlighten me, I'm still learning and yes I do write some pretty ugly code.
Richard MacCutchan 20-Aug-12 4:46am    
The most important thing to do when using any API call is to check the documentation to see what error conditions can occur, and how to detect them. So in your case you should capture the result of each call to _wtof() in a variable and check that it is valid; if not then take some action either to abort the program or perform some recovery mechanism. Once you have captured all your variable values you can then perform your calculation(s) on those results. A side benefit in the above case is that you only need to convert m_sQty7 once.

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