Skip to main content
gamedev.net
🔒 Locked

not responding

Started by sevak 5 replies 1.3k views
Original Post
sevak
sevak
When I run this program it stops responding can someone explain to me why this happens. What this program is supposed to do is find the prime numbers between 1 and 100 #include <iostream> #include <stdlib.h> using namespace std; int main(int argc, char *argv[]) { for(int i = 1;i < 100;i++) { for(int x=0;x <= 9 && x != i;x++) { if((i % x) == 0) cout << i << endl; } } system("PAUSE"); return 0; }

www.computertutorials.org
computertutorials.org-all your computer needs...
serratemplar
serratemplar
Why are you using the system() function? When it bails you should be able to see it...just leave the console window you ran it in open.

Also, there's no reason to keep flushing the stream...that might be pissing your computer off. Use "\n" intstead of endl.
mike25025
mike25025
Unhandled exception at 0x004323a4 in f.exe: 0xC0000094: Integer division by zero.

it happens in this line: if((i % x) == 0)
serratemplar
serratemplar
Oh yeah =) cause x starts at 0 and you immediately divide by it.

You don't want to do that.
Etnu
Etnu
You're doing a modulous operator with a 0 divisor.
---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.
sevak
sevak
O thanks I'll switch x to start at one and see what happens. However, shouldn't the compiler warn me about this?

www.computertutorials.org
computertutorials.org-all your computer needs...
kdogg
kdogg
Compilers generally won't complain when you're dividing by a variable. Although it's obvious looking at the code that x will be 0 when you divide by it, the compiler really doesn't know what value x will have at any given time. Generally, run time errors are up to you to figure out (though a debugger can often help you out).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.