Page 1 of 1
Never ending Program
Posted: Wed Mar 15, 2006 12:39 pm
by imgettionowned
I just started experimenting with loops for c++, so here's a never ending program based upon a for loop.
just compile and run it
Code: Select all
#include <iostream>
int main()
{
using namespace std;
cout << "Press Enter When You Want It To Begin: ";
cin.get();
int a,b,c;
b=-1;
c=100000;
for (a=0;a = b; a++)
cout << c++;
cin.get();
cin.get();
return 0;
}
It's pretty amazing to watch how fast a pc can actually do math, even though it's as simple as adding one
Edit: I am not sure if it is neverending due to restrictions on how big an integer can get
Posted: Wed Mar 15, 2006 2:48 pm
by live2board
If you go "unsigned long a, b, c" you can get alot bigger numbers.
Or to create a never-ending loop just test for a condition you know will never return as true. For example:
Code: Select all
int a = 1;
while (a == 0)
{
cout << "Kevin 0nwz j00!!" << endl;
}
"a" will never evaluate to be equal to zero, therefore it will repeat endlessly.
Posted: Sun Mar 19, 2006 4:24 pm
by Onetoomanysodas
Actually it will do just the opposite.
Since 'a' will never be evaluated to equal zero, the loop statement will never be executed and the program will end.
What would work for an infinite loop is this conditional
Posted: Sun Mar 19, 2006 4:43 pm
by Doctor
I'm not much of C++ but I think this will work, won't it?
Code: Select all
while(1)
{
cout << "[statement] \n";
}
Posted: Sun Mar 19, 2006 4:45 pm
by FleetAdmiralBacon
Code: Select all
void main()
{
yo:
cout << "Bacon owns joo\n";
goto yo;
}
Posted: Tue Mar 21, 2006 5:51 pm
by Onetoomanysodas
Doctor wrote:I'm not much of C++ but I think this will work, won't it?
Code: Select all
while(1)
{
cout << "[statement] \n";
}
Yes that is a common way of processing always true procedures. do() with no while after the ending brace will continue until the condition evaluates false.
FleetAdmiralBacon wrote:Code: Select all
void main()
{
yo:
cout << "Bacon owns joo\n";
goto yo;
}
-- That's nubish >_>
Posted: Tue Mar 21, 2006 6:02 pm
by FleetAdmiralBacon
mb:
it also works, and is the easiest way to make a never ending program.
goto mb;
Posted: Wed Mar 22, 2006 6:08 pm
by live2board
Onetoomanysodas wrote:Actually it will do just the opposite.
Since 'a' will never be evaluated to equal zero, the loop statement will never be executed and the program will end.
What would work for an infinite loop is this conditional
Lol oops. Definately shows how long I've been away from C++. And Goto's are just not cool at all.
Posted: Mon Mar 27, 2006 6:41 am
by xbox7887
Code: Select all
mov eax, 1 ;loop indicator
xor ebx, ebx ;resets ebx
loop:
inc ebx ;ebx+1
cmp eax, 0 ;checks to exit
jne loop ;otherwise loops
This is just a dummied down version of what you're trying to accomplish in x86 assembly. The only thing wrong with it is there is no way to stop the loop. Stopping the process to quit an application isn't always a good idea ;P
Posted: Wed Mar 29, 2006 6:28 pm
by OwnZ joO
Not doubting you, but why is it not always good to end a process?
Posted: Thu Mar 30, 2006 4:45 pm
by xbox7887
Because good applications should be able to quit out themselves...
Posted: Thu Mar 30, 2006 7:20 pm
by Onetoomanysodas
It's not always good to end a windows process for a large application if it is writing data or nested in a loop modifying the memory. This could just cause a program that saves files to lose or corrupt a file while it's buffering it. Sometimes API functions being interrupted or when a program can't find the proper data in memory at the last address it stored to due to a bug in the software or something else can cause windows to alert you or a memory overflow if a loop persists (pretty rare but it can occur as easily in Internet Explorer with an HTML document).