Help to make a loop.

This is a discussion on Help to make a loop. within the C++ Programming forums, part of the General Programming Boards category; Ok, it might seem a little complicated. Please concentrate on the most inner part of the loop. Once i get ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    Help to make a loop.

    Ok, it might seem a little complicated.
    Please concentrate on the most inner part of the loop.
    Once i get to else(), i would like to break out of while() and from the for() loop too.
    But if i put a second "break;" outside of while(), it will break me out of for() even when i come out of if(), which i dont want. Once out of if() i want it to go back to for().
    Maybe this is the case to use the unfamous "goto" ?

    Code:
    #include <string>
    #include <cctype>
    #include <vector>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    void openFile(ifstream& file){
              
            file.clear();
            for(;;)
            {
                string fileName;
                cout << "Enter the name of your file: " ;
                cin >> fileName;
                file.open(fileName.c_str());
                if (file.good())
                {
                    break;
                }
                cerr << "\nThere is no file, named --> " << fileName << " <-- in your folder.\n\n" << endl;
            file.clear();
            }
    }
       
    int main ()
    {   
       ifstream  file1;
       openFile(file1);
       ifstream  file2 ("List of proper nouns.txt");
       ifstream  file3 ("List of proper nouns preceeding words.txt");
       ofstream TheCopy;
       TheCopy.open ("Your_Uppercased_Proper_Nouns_Copy.srt",ios::app);
       string str;
       vector<string> vProperNouns;
       vector<string> vPreceeding;        //vProperNounsPrecedingWords
          
       while (getline(file2, str))
       {        
           vProperNouns.push_back(str);
       }
       file2.close();
       while (getline(file3, str))
       {        
           vPreceeding.push_back(str);
       }
       file3.close();
       
      //write a line into str
       while (getline(file1, str))
       {            
             BreakLoop:
             for ( string::size_type i = 0; i < vProperNouns.size(); i++)
             {     //search for proper noun in str    
                   string::size_type pos = 0;
                   while (( pos = str.find(vProperNouns[i], pos)) != string::npos)
                   {     
                         for ( string::size_type j = 0; j < vPreceeding.size(); j++)
                         {   //search for preceeding word before proper noun
                             string::size_type loc = 0;
                             while((loc = vProperNouns[i].rfind(vPreceeding[j],pos)) != string::npos)
                             {
                                 // the one it found is just not before the proper noun: go back to for().
                                 if ( loc < (pos - (vPreceeding[j].size()+ 1))) 
                                 {    
                                     break;
                                 }  
                                 //the one it found is just before the proper noun: break out of while() and out of for() too.
                                 else  
                                 {                                            
                                     pos += str.size();
                                     goto BreakLoop;
                                 }              
                             }
                          loop: break;   
                         }
                   str[pos] = toupper (str[pos]);               
                   }
             }
             cout<<str<<endl;
             //TheCopy << str << endl;
       }
       file1.close();
       TheCopy.close();
       system("pause");
       return 0;
    }
    Last edited by Ducky; 03-09-2008 at 06:15 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    One way would be to uses goto and jump to a label named "BreakLoop" or something descriptive along the lines of that. Put it directly outside the loop.
    Otherwise you need a variable and use two breaks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thank you!

    Quote Originally Posted by Elysia View Post
    Otherwise you need a variable and use two breaks.
    A variable? To do what with it?
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    A variable? To do what with it?
    To control the outer loop.

    There is another possibility: to place the relevant code in a function and call that function. Of course, the difference is arguably cosmetic: instead of a goto with a label after the loop, you use a return. On the other hand, this may actually help to make the code easier to read by abstracting away a block of code into a function, thus providing it a name.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thank you Laserlight!
    This function solution sounds good but a bit hard for me to write it right now, maybe later.

    This loop control with a variable, i never heard of it.
    In what section of any tutorials should i search for it?
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    This loop control with a variable, i never heard of it.
    In its simplest form:
    Code:
    bool flag = true;
    while (flag)
    {
        // ...
    }
    In reality, what to use depends on the situation. You could even use one such variable that participates in the loop condition of all the loops (with &&).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thanks, i'll try that one.
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21