How to exit a loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: How to exit a loop

  1. #1
    Join Date
    Jan 2009
    Posts
    1

    How to exit a loop

    I am doing a homework assignment in which we have to get an undetermained amount of numbers from the user. I was wondering how to tell when the user is done entering numbers and wants to exit the loop, such as perhaps a blank space or a word like 'quit'.

    Here is what I have so far, I used 72 as the exit number just as a test.
    Code:
    // Function Used to Get Numbers into Array
    int  calcList() {
    
    	for (int i=0; i<100; i++) {
    		std::cout << "Enter an integer: ";
    		 std::cin  >> list[i];
    		 counter = counter + 1;
    		 
    		 // Exit Loop When 72 is Inputed
    		 if (list[i]== 72) {
    			 return(0);
    		 }
    		 
    	}
    }
    Thank you.
    Last edited by Andreas Masur; January 18th, 2009 at 09:06 PM. Reason: Added code tags...

  2. #2
    Join Date
    Jan 2009
    Posts
    14

    Re: How to exit a loop

    This code doesnt work? and why you used of counter = counter + 1;?

  3. #3
    Join Date
    Nov 2006
    Posts
    221

    Re: How to exit a loop

    when u need to exit from loop just put your exit condition there
    means your loop run with i<100 so just write i = 101 in ur if block

    Thanks

  4. #4
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Lightbulb Re: How to exit a loop

    I think u can accept the nos as "strings" form the user... parse through every string and look if any char(other than digit is entered by the user use IsDigit() call to do that) if any char is found in the string just come out of the loop else Convert the string using atoi() fun and store in a integer array.

  5. #5
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: How to exit a loop

    If you need to stop the input getting cycle on a determinate input, before the cycle exausts its cycles you can simply use the instruction break, which iterrupts the inner cycle. Returning 0, as in the snipped you post, has the same effect, but just in this case!
    It not only, in fact, terminate the cycle, but also the function, so, if after the input cycle there have been some code that needed to be executed, like a message that says "thanks", it would be jumped out.

    Also, as elsewhere said, counter is useless; the cycle counter, i, is in fact the same as counter, which is resdundant, and will be easily considered an error. Also, you have not initialized counter; it could be a variable declared visible to all your application; in that case, to update its value, I would do as follows:

    Code:
    // Function Used to Get Numbers into Array
    int  calcList() 
    {
    	for (int i=0; i<100; i++) &#249;
           {
    		std::cout << "Enter an integer: ";
    		 std::cin  >> list[i];
    		 
    		 // Exit Loop When 72 is Inputed
    		 if (list[i]== 72) 
    			 break;   //  {} are not necessary for single instruction ifs; they only make the program less readable
    	}
            counter = i; //This sets counter to i, the ordinal of the last number inserted
    }
    Above, when inserted 72, program execution instantly exits the loop, and update counter with the number of entries inserted. If you need to call calcList() more than once, and you need that each time counter contains the subtotal of all numbers inserted during the execution (not of a single call ti calcList(), like in the program above) you just need to change the last line with

    Code:
    counter+=i;  //Add current number of values to current subtotal
    Last edited by Buzzyous; January 22nd, 2009 at 09:18 AM.
    - Buzzyous -

  6. #6
    Join Date
    May 2008
    Location
    Michigan
    Posts
    2

    Re: How to exit a loop

    Hey neoseeker191,

    Another way you can code this is by using a "flag-controlled" loop. Its a simple concept. The way it works is:

    1. You declare a Boolean variable, in this case "seventyTwo."
    2. You then give that Boolean a value of either "true" or "false."
    3. You run a while loop where the value is held true, until the Boolean's value is purposely changed through your command within the loop using an "if statement."
    4. If you originally declare your Boolean variable to true, you want to set that variable to false within the if statement within the while loop which will then return a value of "0" or "false" to the while loop. This will then terminate (exit) the loop.

    I hope that explanation helped. Well here is the code:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std ; 
    
    int main()
    {
    	int list[150] ;
    	int counter ;	
    	int i ;
    	bool seventyTwo ;
    	counter = 0 ;
    	seventyTwo = true ;
    	
    	for (i = 0 ; i < 150 ; i++)
    	{
    		while(seventyTwo)	// While flag seventyTwo is true - loop
    		{
    			cout << "Enter an integer: " ;
    			cin >> list[i] ;
    			counter++ ;	// Increments counter by a value of 1
    			if (list[i] == 72)	
    			{
    				seventyTwo = false ;	// Terminates loop upon entry of integer 72
    			}
    		} // End of while loop (inner)
    	} // End of outter for loop (outer)
    	
    	return 0 ;
    
    } // End of main
    Also just for more information the code line:
    Code:
    counter++ ;	// Increments counter by a value of 1
    is the same thing as coding:
    Code:
    counter = counter + 1 ;
    It is just a quicker way of stating that. Also, this was a combination of loops, also referred to as "nested loops." The while loop was "nested" within the for loop. Anyways, I hope this helps. Good luck on your assignment.

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,062

    Re: How to exit a loop

    Quote Originally Posted by Enfinik View Post
    I hope that explanation helped. Well here is the code:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std ; 
    
    int main()
    {
    	int list[150] ;
    	int counter ;	
    	int i ;
    	bool seventyTwo ;
    	counter = 0 ;
    	seventyTwo = true ;
    	
    	for (i = 0 ; i < 150 ; i++)
    	{
    		while(seventyTwo)	// While flag seventyTwo is true - loop
    		{
    			cout << "Enter an integer: " ;
    			cin >> list[i] ;
    			counter++ ;	// Increments counter by a value of 1
    			if (list[i] == 72)	
    			{
    				seventyTwo = false ;	// Terminates loop upon entry of integer 72
    			}
    		} // End of while loop (inner)
    	} // End of outter for loop (outer)
    	
    	return 0 ;
    
    } // End of main

    What is this supposed to do?
    Your inner while loop will keep looping until the user enters 72 and on every loop it will overwrite the number in the list at position i. Then when the user enters 72, you exit the while loop BUT continue the for loop and this 150 times... doesn't sound right to me

  8. #8
    Join Date
    May 2008
    Location
    Michigan
    Posts
    2

    Re: How to exit a loop

    You are totally correct. I didn't even think about that potential issue. ( I am new also @ C++, but felt I could help on this..) Thanks for letting me and neo know this. I am going to probably go back and work out the kinks on the loop later, and maybe re-post. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center