Program crashing around for loop

Hello! The snippet of code below keeps crashing right after "person[j].relationship = true;" (the cin.get()'s were just for debug purposes), at least that's what it seems like. The rest of the code has worked perfectly. Does anyone have any ideas as to why it's crashing? Thank you for your help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  else if(person[j].relationship == false)
        {
            if(person[j].age < 18)
            {
                person[j].sig_other = j;
                person[j].relationship = true;
                cout << "UNDER AGE" << endl;
            }
            else if(person[j].age >= 18)
            {
                random_sex = rand()%82+18;
                for(int i = 0; i < num_people || person[j].relationship == true;i++)
                {
                    if(person[i].age == random_sex && person[i].relationship == false)
                    {
                        cout << "MATCH MADE" << endl;
                        person[i].sig_other = j;
                        cin.get();
                        person[i].relationship = true;
                        cin.get();
                        person[j].sig_other = i;
                        cin.get();
                        person[j].relationship = true;
                        cin.get();
                    }
                    else
                    {}
                }
                if(person[j].relationship == false)
                {
                    person[j].sig_other = j;
                    person[j].relationship = true;
                    cout << "NO RELATIONSHIP FOUND" << endl;
                }
                else
                {
                    cout << "RELATIONSHIP ASSIGNMENT ERROR" << endl;
                }
            }
            else
            {
                cout << "RELATIONSHIP AGE ERROR" << endl;
            }
        }


Edit: The "person[j].relationship = true;" is within the for loop.
Last edited on
Hi,

could you show us the output (or maybe the error you are getting?)

also is there any reason for line 26 and 27?
Thank you for responding.

I'm using Code::Blocks and after the program crashes, it gives me:
"Process returned -10737418119 (0xC0000005)." Otherwise there isn't anything else it gives me.

There isn't really a reason for those lines. Just habit to combine ifs with elses.
closed account (E0p9LyTq)
I have to ask: Which person[j].relationship = true;? I see three instances of that statement: lines 6, 23 and 32.
Running it through the debugger gets me this: http://imgur.com/a/a4Wxz
_____
The one within the for loop
Sorry for the double post. It seems like I've resolved the for loop crash (to be honest, I'm not entirely sure because of the following), but is the code snippet below not valid? How would I go about creating a condition for the if statement that would make it so that each element is taken into consideration?

1
2
3
4
5
                    for(int q=0; person[j].relationship == false; q++)
                    {
                        condition1 = ((person[q].relationship == false) && (j != q));
                        condition2 = (person[q].age >= person[j].age - 10) && (person[q].age <= person[j].age + 10);
                        if(condition1 && condition2)
So in other words, your program loops forever, or crashes?
It would just crash when the code was what it was in the original post. I've since used the built in debugger, which was not providing me with anything I could use. I've also moved some of the conditions out to try and compartmentalize things to see if that could help, but it hasn't. This is what it looks like now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
               for(int q=0; person[j].relationship == false; q++)
               {
                   if((person[q].relationship == false) && (j != q))
                   {
                       condition1 = true;
                   }
                   else
                   {
                       condition1 = false;
                   }

                   if((person[q].age >= random_sex - 5) && (person[q].age <= random_sex + 5))
                   {
                       condition2 = true;
                   }
                   else
                   {
                       condition2 = false;
                   }

                   if(condition1&&condition2)
                   {
                       cout << "     MATCH FOUND" << endl;
                       cout << "     #" << j << "->" << q << endl;
                       matches++;
                       person[q].relationship = true;
                       person[q].sig_other = j;
                       person[j].sig_other = q;
                       person[j].relationship = true;
                   }
                   else if(q >= num_people)
                   {
                       cout << "     NO MATCH FOUND" << endl;
                       person[j].sig_other = j;
                       person[j].relationship = true;
                   }
                   else
                   {
                       //cout << "     DEFAULT TO ELSE" << endl;
                   }
               }


Everything compiles fine, it's just at runtime that it crashes and I've been able to narrow it down to the above portion. It's been stumping me all day, but I can't seem to get either the if statement to work as I want it to or the for statement to even work when the if kind of works. Other times it runs fine, but the results default to the else if statement.
I cannot keep track of the value of (j) since I don't know what the original value of (j) is yet.

Maybe you post your entire code here instead?
If it is inside a function, maybe only posting the whole function will help.
Sorry, away from my computer now, but that snippet is within another for loop that just runs through each object associated with the struct in another file. It's initialized in main using a vector ("vector<People> person(num_people)"[num_people=100]). I can update this post with the rest of main tomorrow (or all of the files in a zip) if it'll help, but I know that the rest of it worked fine as this part I'm working on now came after the rest of it.
Oh, that's enough.

for(int q=0; person[j].relationship == false; q++)

Change it to :
for(int q=0; (person[j].relationship == false && q < num_people); q++)
Last edited on
Does that help? :)
I'm sorry. It works, but it's not necessarily what I'm going for. The loop terminates once it hits num_people by changing person[j].relationship to true (which is what I want to happen). And as I thought, I was missing something obvious with the conditions in my if statement. As the program runs, it was assigning person[j].relationship to true without opening it up to later revision assuming other factors. Seeing the change you offered, though, helped it click in my head, so thank you very much! I feel silly :P

At any rate, the loop crashing is solved. I was attempting to read person[q] when 'q' was greater than num_people, which limited the size of person[]. The if statement needed other conditions (and more use of parentheses) to limit what got compared properly.
Topic archived. No new replies allowed.