i broke while

Started by
34 comments, last by Tron3k 19 years, 6 months ago
I'd like to take this time to point out that, if you're using Vis Studio or any number of other quality compilers, you have a useful tool called a debugger. Use it. Going with the Vis Studio interface, that's done as follows:

- Right click on some code before the start of your loop
- Click "Run to cursor"
- The program will run, and then pause at the line you selected, returning you to your source code.
- Press F10 to step through your code, executing one line at a time.
- When you come to your loop, what happens? Does it skip over? Does the debugger come crashing down? Does the current-line cursor rotate around and around within the loop, indicating that your while is in fact working fine, and just not do anything, indicating that you have some other problem?
Advertisement
Quote:Original post by iMalc
'while' isn't broken, it's your printf that isn't outputting anything.
Just use the keyword as you would normally instead of making unrealistic loops that don't flush the output buffer or something.


How often do you see fflush statements in console programs? I can tell you, not very often, because you usually don't need to flush the output buffer manually.

Unless the internal buffer is really huge, there would be forced flushes in a loop like that. Even a MB buffer would fill up and be flushed quite fast. So its very unlikely that it's buffering that causes his problem.

And now back to the real problem

I still think the real problem is somewhere else in the function, either that it doesn't enter the loop at all, or that he is corrupting the stack somehow. Jtrask is right about using the debugger, sooner or later you have to learn to use it. A problem like this should be easilly detectable in the debugger. But if not then more source code is needed.
Try fflush, if not, maybe you accidently did something silly like while(1); {...}.

When I used msys in Windows, it was really anal about flushing your buffers. It would probably hold several kilobytes of text before it would get around to finally displaying it, which usually resulted in you not seeing anything and thinking there was something horribly wrong with your program.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
If it's a console app, it's almost certainly the \n issue - I'm reasonably sure that whatever the stdio streams do, the console window itself doesn't display text until it receives an end-of-line character (\n works, iirc, although endl is better).
Quote:Original post by fractoid
If it's a console app, it's almost certainly the \n issue - I'm reasonably sure that whatever the stdio streams do, the console window itself doesn't display text until it receives an end-of-line character (\n works, iirc, although endl is better).


That's not true, at least not in winxp. I just did a quick test with visual studio 2003.

[source}int main(){	while(1)	{		printf("this");	}	return 0;}


and it prints "this" forever. And I dont seem to remember that this was the case when I used vc++ 6 and other versions of windows either.
if you are compiling in MSVC++ in C source mode, make sure and include the proper standard C libraries. I had a problems with standard c functions in C mode returning utter garbage ( one of wich was printf() not behaving properly) , and i switched to c++ and the compiler fussed because I was not including the proper libraries. It seems like a stupid mistake, but it happens.

[edit] i switched back to C mode and it worked perfectly [/edit]
I'll bet almost anything that either he is typing while(1);{},
or...

Someone is playing a trick on you and has defined while as:

#define while(x) while(x);


Both of those would explain why do-while is working as well.
Not giving is not stealing.
no, my form is perfect, i've tried amany a sample applications.
| Member of UBAAG (Unban aftermath Association of Gamedev)
When do you exit your while-loop? Maybe it never exits and the CPU is to busy to print the text. Try:
short c = 1; //loop counterconst loops = 10; //maximum loopswhile (c < loops){printf("this");c++;}


(sorry if i missed someone already wrote something like this)

\Jimmy H
"no, my form is perfect, i've tried amany a sample applications."

apparently its not. Show us the source code to your tests, letter for letter. what compiler are you using? what kind of project? what includes? what static libs?

your form is most likely NOT perfect if you managed to break one of c++'s most used keywords... ive never heard of this before... which has me convinced its human error

-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."

This topic is closed to new replies.

Advertisement