do while + while = hang?

Hi.. can someone help me out? is it possible to have a while loop within a do..while loop? because i have one in my program now and it hangs when its time to do the loop.. so im wondering if its possible.. Thanks!
Yes, it's perfectly possible.

If your program hangs, it's likely you haven't coded the loop conditions properly and one of the loops is continuing forever.
closed account (owX8T05o)
Can you be a little more precise and / or copy paste your 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void Product::Delete()
{
     Product del, infos;
     bool found = false;
     fstream file("h2td.txt", ios::in);
//***********************************************************************  
     cout << "Enter Code to Search: ";
        getline(cin,scode);
     do
     {
         getline(file, z, '~');
         getline(file, code, '~');
         getline(file, gName, '~');    
         getline(file, price, '~');
             if (scode!=code)
             {
                 found = false;
 //               while(!file.eof() && found == false)  //if i remove this, then the program continues on. with it, its just stuck there..
 //               {
                 fstream file2("temp.txt", ios::out | ios::app);
                 file2 << z << '~';
                 file2 << code << '~';
                 file2 << gName << '~';
                 file2 << price << '~' << endl;
                 file2.close();
 //                };
             }
             else
             {
                 found = true;
             }
     }while(!file.eof() && found == false);
//************************************************************************
         if (found == true)
         {
            cout << "Record Found!" << endl;
                 cout << z << "\nCode: " << code;
                 cout << "\nGeneric Name: " <<  gName;
                 cout << "\nUnit Price: " << price;

//*******************************************************************
           cout << "\n" << "-----------------------------------------------------------------" << endl;
           isthis();  // returns bool true if this is the record that the user wants deleted
           if(thiz == true)
           {
              cout << "Record Deleted!" << endl;
              file.close();
              remove("h2td.txt");
              rename("temp.txt","h2td.txt");
           }
         }
         else
             {  cout << "Record not found" << endl;  }
     file.close();
     system("PAUSE");
     Menu();
}


Im this in the process of trial and error with trying to delete... and this is something that i was thinking might work...Please do correct me if im wrong...
1. Search for a code
2. search for the code
3. as long as the record's code is not the same as the search code, write the record to the temp file
4. if found, display it
5. continue writing other records to the temp file <- this is where im having problems
6. ask the user if the displayed record is the one that the user wants deleted.
7. if yes, delete the old file and rename the temp file.

The contents of my file is something like this:

~111~paracetamol~15~
Last edited on
I noticed that on that first do while loop, one of the conditions is if found == false, but you don't have any anything that changes it from that particular condition once it's run, so it continues running that loop.
closed account (owX8T05o)
Bien vu ! ;)
im still having the same prob..any other suggestions?
The problem is in your exiting conditions:
while(!file.eof() && found == false)

found is obviously false since it enters this once, so the issue must be with the file.eof. This condition is set outside the while loop and so the condition never changes. file.eof is ALWAYS false so the loop will never exit. Actually, you don't even need a while loop here since you don't need to continue sending anything to file2 (otherwise you'll send the same thing over and over).

Try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
     do
     {
         getline(file, z, '~');
         getline(file, code, '~');
         getline(file, gName, '~');    
         getline(file, price, '~');
             if (scode!=code)
               {
                found = false;
                if(!file.eof() && found == false)  
                 {
                 fstream file2("temp.txt", ios::out | ios::app);
                 file2 << z << '~';
                 file2 << code << '~';
                 file2 << gName << '~';
                 file2 << price << '~' << endl;
                 file2.close();
                 };
             }
             else
             {
                 found = true;
             }
     }while(!file.eof() && found == false);


Thank you! that finally got me out of the loop.. but sadly, its still not doing what i want... sniff sniff.. is my logic wrong? or am i missing something?
Topic archived. No new replies allowed.