An alternative to Do While loops?
Hello again everyone 
I haven't posted in awhile, but I've been lurking...
I've run across some (C++) code that I have once before a while loop begins executing and then again at the end of the loop. The solution seems to be to use a do...while loop, but I hate do while loops. In my opinion, they look horrible and are much harder to read than normal while loops.
Any ideas on a different solution that is cleaner and better looking? Perhaps I'm just being stubborn, but I really don't like do while loops
Here's the code that I noticed this in:
Thanks
-wyrmmage

I haven't posted in awhile, but I've been lurking...

I've run across some (C++) code that I have once before a while loop begins executing and then again at the end of the loop. The solution seems to be to use a do...while loop, but I hate do while loops. In my opinion, they look horrible and are much harder to read than normal while loops.
Any ideas on a different solution that is cleaner and better looking? Perhaps I'm just being stubborn, but I really don't like do while loops

Here's the code that I noticed this in:
Code:
frame tempFrame;
tempFrame.texture = -1;
std::string tempString = "";
std::string dataLine;
getline(animReadFile, dataLine);
char* stringPoint;
while(dataLine.size() > 0)
{
for(stringPoint=(char*)dataLine.c_string(); *stringPoint!='\0'; stringPoint++)
{
//some code
}
//more code
tempFrame.texture = -1;
tempString = "";
getline(animReadFile, dataLine);
}Thanks

-wyrmmage
Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Here's a way, .... not saying it is any cleaner
.... but less code, and no do-while.
.... but less code, and no do-while.Code:
frame tempFrame;
tempFrame.texture = -1;
std::string tempString = "";
std::string dataLine;
char* stringPoint;
while ( 1 ) {
getline(animReadFile, dataLine);
if ( dataLine.size() <= 0 ) {
break;
}
for(stringPoint=(char*)dataLine.c_string(); *stringPoint!='\0'; stringPoint++)
{
//some code
}
//more code
tempFrame.texture = -1;
tempString = "";
}
Well, that is definitely an alternate solution 
Still, as you said, it's not exactly a clean solution...but I still like it better than do while loops
Any other suggestions?
Thanks,
-wyrmmage

Still, as you said, it's not exactly a clean solution...but I still like it better than do while loops

Any other suggestions?
Thanks,
-wyrmmage
Worlds at War (Current Project) - http://www.awkward-games.com/forum/
wyrmmage Wrote:Any other suggestions?
Learning not to hate do while loops would be the best solution. They're part of the core language syntax, so why avoid them arbitrarily?
Plus, those aren't do-while loops.. (unless I'm misunderstanding what you're saying?)
But yeah, why avoid them? Your code looks more messy because of the indenting than the while loop.
But yeah, why avoid them? Your code looks more messy because of the indenting than the while loop.
I know they aren't do while loops; I was just saying that I was going to convert my code to use do while loops to reduce code duplication.
Personally, I like my style of indentation better, but each to their own
I guess I'll just learn to love do while loops, then
Thanks everyone
-wyrmmage
Personally, I like my style of indentation better, but each to their own

I guess I'll just learn to love do while loops, then

Thanks everyone

-wyrmmage
Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Even with a do-while, you are still going to have to (or should) check after the first read, and then again at the bottom of the loop
Code:
1st read
if ( dataLine.size() > 0 ) {
do {
// some code
2nd read
} while ( dataLine.size() > 0 )
} // endifCode:
for(getline(animReadFile,dataLine); dataLine.size()>0; getline(animReadFile,dataLine)) {
// some code
}The full example would be:
Code:
frame tempFrame;
tempFrame.texture = -1;
std::string tempString = "";
std::string dataLine;
char* stringPoint;
for(getline(animReadFile,dataLine); dataLine.size()>0; getline(animReadFile,dataLine))
{
for(stringPoint=(char*)dataLine.c_string(); *stringPoint!='\0'; stringPoint++)
{
//some code
}
//more code
tempFrame.texture = -1;
tempString = "";
}
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| PasteAll.org: A prettier alternative to RAFB | Leisure Suit Lurie | 2 | 2,633 |
Apr 10, 2008 04:09 PM Last Post: Duane |
|
| Alternative to a global VLA | ferum | 6 | 3,723 |
Jul 24, 2006 06:47 PM Last Post: OneSadCookie |
|

