Thread: reverting to a while loop

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    reverting to a while loop

    I have a program with a while loop that searches an array. I want to run the while loop, and then based on the outcome of the search, output a statement to the user.

    I don't want the output statements within the while loop, but if the user wants to search again, I need to get back into the loop.

    How can I do this?

    Brian

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Put the while loop in a while loop?

    Code:
    
    exit=false;
    while(!exit)
    {
      while(...)
         ....;
      cout << "Run again?";
      cin >> YN;
    
      if( YN == "n" )
        exit=1;
    }
    Best Regards,

    Bonkey

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    make your while loop into a function and call it within your program.

    Code:
    int search_array();
    int x;
    
    int main{
    
    int quit = 0;
    
       while (quit == 0){
          search_array();
          
          switch(x){
             case 1:
             //output 1
             break;
             //etc...
          }
          
          char a;   
          cout << "search again? (y/n) ";
          cin >> a;
          if (a == n) quit = 1;
        }
    
    int search_array(){
       //search the array and return x 
    }
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    on the contrary

    Both posts helped. Program is working now. Thanks a lot.

    Brian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM