Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am having a trouble looping through a 2d array using a for-loop
my program breaks after doing first comparison and i can't figure out why.
here is my code

C++
    readData();
    sortData();
    
    cout << ID_EMEI[5][1] << endl;

    for (int r=0; r<20; r++)
    {
        if (ID_EMEI[r][0] == ID)
        {
            if(ID_EMEI[r][1] == EMEI)
            {
                 cout << ID_EMEI[index][0] << "-";
                 cout << "Customer found, you can give them their belongings"<<endl;
                 cout <<"please write a short report about the phone" <<endl;
                 cin >>report;
                 
                 Write(ID, EMEI, report);
            }
        }
    }
Posted
Updated 30-Aug-14 8:33am
v2
Comments
nv3 30-Aug-14 14:36pm    
Hard to tell what is going wrong without seeing how the array is declared. So please show also the line, where your array is declared and defined.

Also: Why don't you step through your program in a debugger. That will pretty quickly show you what's going wrong.
Dirquez Marquez 30-Aug-14 14:48pm    
array declaration:

int ID_EMEI[20][2];

//hre i am storing data to the array reading from a long string of elements separated by "-"
while(getline(breaker, temp_string, '-')){ ///this is a way for splitting

if(word1.length() < 1)
word1 = temp_string;

else if(word1.length() > 1)
word2 = temp_string;

istringstream(word1) >> ID_EMEI[index][0];
istringstream(word2) >> ID_EMEI[index][1];

index +=1;

}
Stefan_Lang 2-Sep-14 8:46am    
Reading this, and combining it with the suggestion and question posed in solution 1:

the last statement here is index+=1
I don't see where index is being initialized or how it's controled to stay in range, but that last statement may well push the index beyond the range of the array, and in the code you posted above you use that index to access the array. Have you checked that code is actually correct? did you mean to use r instead? If not, have you verified index is less than 20?

1 solution

The only line that catches my eye is
C++
cout << ID_EMEI[index][0] << "-";

Why are you using index here instead of r? index might in fact stand one beyond the array after your input loop (which is not shown in your code extract).
 
Share this answer
 
v2

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