writing two while loop

Hi,

I am uncertain if i can place a while loop inside a while loop, although trying it proves me that it cannot be done.

Is there another work around method? I did not paste the whole codes down, just the crucial part, thanks for any help rendered.

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
    map<string, int>::iterator p = count.begin();
    map<string, int>::iterator e = count.end();
    
    while (p != count.end())
    {
          if (p->second >= threshold) 
          countCommon++;
          else countIndex++;
          p++;
    
    index << "Total words: " << countIndex << endl;
    common << "Total words: " << countCommon << endl;

    while (p != count.end())
    {
        if (p->second >= threshold)
        
             common << p->first 
                    << "\t" 
                    << p->second 
                    << endl;

        else 
             index << p->first 
                   << "\t" 
                   << p->second 
                   << endl;
        p++;
     }
   }
        


how it should look like:

Example
total words: 3
elephant 2
dogs 2
bees 4
Last edited on
You can put while loops in while loops. That works fine.

The problem with your code is that both your while loops have exactly the same condition. So both loops will terminate at the same time.

I imagine that is not what you intended.
Last edited on
+1 galik

Hi Galik,

Thanks for your help in bringing me to the right direction, solved it. ",
Last edited on
Topic archived. No new replies allowed.