Help with While and If loops

when we run this section of code, we get an infinite loop at c. The lines cout << b, c, and d are markers in the code to see where we are getting to, they can be ignored.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   rcountera = 0;
   rcounterb = rcountera + 1;
   match = false;
   while((rcountera < 23))
         {
               while ((match == false) && (rcounterb < 23))
               {
                     if (roomarray[rcountera] == roomarray[rcounterb])
                     {
                         matchcount = matchcount + 1;
                         cout << "b";
                     }
                     else
                     {
                         rcounterb + 1;
                         cout << "c";
                     }
               }
               cout << "d";
               rcountera + 1;
               rcounterb = rcountera + 1;
         }
Last edited on
Your internal while loop never updates either of the variables that affect its logical conditions. If it gets into the inner loop at all, it is going to stay there. The closest you come to updating one of the variables that affect your loop conditions is on line 15 where you say rcounterb. This adds 1 to the value of rcounterb, but this result is thrown away. Perhaps you meant rcounterb = rcounterb + 1;?
Last edited on
A couple things here:

First of your statements that look like this:

rcounterb + 1;

do nothing to increment the variable. What you mean to do is:

rcounterb++;

or

rcounterb += 1;

. . . Same for rcountera. . .

Additionally, in your while statement, you mention that it should keep looping as long as match is equal to false, and yet you never do anything to switch that statement to true within the loop, so that "match" will always be false and thus the loop will never terminate on account of the "match" variable.
Right, that part works now, thank you all. However, there is a new problem. We are trying to calculate matches for a multitude of arrays, with random values on each array value each time. Whenever we run this code. matchcount equals the number of trials exactly, every time, no matter the number of trial.

Here is the code:
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
     for (trialcounter = 0; trialcounter < trialnumber; ++ trialcounter)
     {    
         const int roomsize = 23;
         int roomarray[roomsize], roomcounter;
         srand (time (0));
         for (roomcounter = 0; roomcounter < 23; ++ roomcounter)
         {
            roomarray[roomsize] = (rand ()% 365) + 1;
         }
         rcountera = 0;
         match = false;
         while (rcountera < 22)
         {
               rcounterb = rcountera + 1;
               while ((match == false) && (rcounterb < 23))
               {
                     if (roomarray[rcountera] == roomarray[rcounterb])
                     {
                         match = true;
                         matchcount++;
                     }
                     else
                     {
                         rcounterb ++;
                     }
               }
               rcountera ++;
         }
     }
     percent = (matchcount/trialnumber)*100;
Topic archived. No new replies allowed.