C How to Start a for Loop Again

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 22-February 10

Looping back to start of while loop

Posted 19 October 2010 - 01:05 AM

Hi. I'm very new at C++. I need helping in this function to return to the top of the while loop. I've said in comments where this change needs to be done (within the inmost if statement). Any suggestions as to how I should go to the start of the while loop?
This function works how I want it to during the first iteration, but I can't get it to return to the top.

Please help!
Thanks in advance!

bool check(string way) { 	string copy = way; 	while(copy.size()>=0) 	{ 		if(copy == "") 			return true; 		if(copy.size() == 1) 			return false; 		if(copy.at(0) == 'n' || copy.at(0) == 'N' || copy.at(0) == 's' || copy.at(0) == 'S' || copy.at(0) == 'e' || copy.at(0) == 'E' || copy.at(0) == 'w' || copy.at(0) == 'W') 		{ 			if(isdigit(copy.at(1))) 			{ 				if(isdigit(copy.at(2))) 				{ 					copy = copy.substr(0,2);                                         //HERE 				} 				else 				{ 					copy = copy.substr(0,1); 				        //HERE                                 } 				return true; 			} 			else 				return false; 		} 		else 			return false; 	} }            


Is This A Good Question/Topic? 0

  • +

#2 Banfa User is offline

Reputation: 83

  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: Looping back to start of while loop

Posted 19 October 2010 - 01:13 AM

You can use the continue statement, look it up.

But rather than put it at lines 17 and 22 just put it at line 24 in place of the return TRUE;.

It should be noted that use of the continue is frowned on in some coding standards as it can make program flow hard to follow. Loops can normally be re-written not to require it and in fact in the case of your code you do not need the continue statement at all as you could just remove the return TRUE; at line 24 to achieve the same thing.

BTW what exactly is this function meant to do?

This post has been edited by Banfa: 19 October 2010 - 01:15 AM

#3 lakers1230 User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 22-February 10

Re: Looping back to start of while loop

Posted 19 October 2010 - 01:20 AM

IT WORKED!! THANK YOU SO MUCH!!

This post has been edited by lakers1230: 19 October 2010 - 01:27 AM

#4 janotte User is offline

Reputation: 991

  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Looping back to start of while loop

Posted 19 October 2010 - 01:33 AM

This scares me a little:

while(copy.size()>=0)            

Do you really want to enter that loop with a string length of zero?

#5 lakers1230 User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 22-February 10

Re: Looping back to start of while loop

Posted 19 October 2010 - 01:37 AM

View Postjanotte, on 19 October 2010 - 12:33 AM, said:

This scares me a little:

while(copy.size()>=0)              

Do you really want to enter that loop with a string length of zero?

Yea, I'm fine with that because my first if statement returns true if it's an empty string.
I'm essentially trying to make this a recursive type function. I've done this in Java, but I'm new to C++, so I'm not well versed as to how to take care of business.

#6 janotte User is offline

Reputation: 991

  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Looping back to start of while loop

Posted 19 October 2010 - 02:04 AM

View Postlakers1230, on 19 October 2010 - 05:37 PM, said:

Yea, I'm fine with that because my first if statement returns true if it's an empty string.

Can I suggest a couple of tweaks.
Feel free to ignore them.

1 - Since the .size() is always going to be zero or greater (can't return a negative)
http://www.cplusplus...ng/string/size/
your test will always resolve to true so the loop is an infinite loop.
Now there is zero wrong with an infinite loop but rather than a complex test that confuses tiny brains like mine it is more normal to make it clear you are setting up such a loop on purpose by using this syntax

while(true){            

That means anyone glancing at your code knows you have deliberately set yourself up an infinite loop and haven't done so by accident.

2 - While this

if(copy == "")            

will work it is a little odd way of describing an empty string and in the next line you use a completely different method of working out if the string is not empty. It does mean the reader's brain has to flip through a couple of different abstractions when using a single abstraction could make things easier to grasp and read on first pass. Can I suggest you make use of the .size() function here to make it clear what you are doing. Like this:

if(copy.size() == 0){            

which can be further refined to this

if(!copy.size()){            

if you really want to.
Make sense?

As I say, neither of these change your code's behaviour but the first one will definitely make it easier to understand and the second will arguably make it easier to understand on a first read through. But, again, feel free to ignore it all.

#7 lakers1230 User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 22-February 10

Re: Looping back to start of while loop

Posted 19 October 2010 - 02:17 AM

Oh ok definitely. I took those into account.
I used the first one to clear things up.
But for your second piece of advice, my professor specifically told us to search for an empty string. I totally understand where your points are coming from, but I think I should stick as close to the specifications as possible.

Thanks! :)

#8 janotte User is offline

Reputation: 991

  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Looping back to start of while loop

Posted 19 October 2010 - 02:26 AM

View Postlakers1230, on 19 October 2010 - 06:17 PM, said:

I think I should stick as close to the specifications as possible.

Very, very wise.
I completely agree.:tup:

parsonwerme1946.blogspot.com

Source: https://www.dreamincode.net/forums/topic/195689-looping-back-to-start-of-while-loop/

0 Response to "C How to Start a for Loop Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel