I have been trying to create my own prime checker function, although strangely when I call isPrime(7) it returns 1, which is good, but when I call isPrime(9) it gives me the following error:
'Mathematics.exe': Loaded 'C:\Documents and Settings\mbryant\My Documents\Visual Studio 2010\Projects\Mathematics\Debug\Mathematics.exe', Symbols loaded. 'Mathematics.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Cannot find or open the PDB file 'Mathematics.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Cannot find or open the PDB file 'Mathematics.exe': Loaded 'C:\WINDOWS\system32\msvcp100d.dll', Symbols loaded. 'Mathematics.exe': Loaded 'C:\WINDOWS\system32\msvcr100d.dll', Symbols loaded. The thread 'Win32 Thread' (0x6ec) has exited with code -1073741510 (0xc000013a).
Here is the code:
#include <iostream>
using namespace std;
bool isPrime(int x){
int b = 0;
int i = 2;
if(x == 2){
return 1;
}
if (x > 2){
while(i < x){
if ( (x % i) != 0){
b = b + 1;
i = i + 1;
}
}
if (b > 0){
return 1;
} if (b == 0){
return 0;
}
}
}
int main(){
cout << isPrime(9) << endl;
system("pause");
return 0;
}
Helping with resolving this issue would be greatly appreciated.
According to:
Jobs failing on Windows with Exit Code 0xC000013A
Globally speaking, Exit Code 0xC000013A means that the application terminated as a result of a CTRL+C or closing command prompt window
I copied, compiled and ran your code. With x=9
, the code is stuck in the while
loop forever, so I had to close the program using the close button ([x] button in the upper right corner). That generated the 0xc000013a error code. (With x=7
the program is not stuck in the while
loop so it is able to exit normally.)
More specifically, for x=9
the program is stuck in the while
loop because when i=3
then (x % i) == 0
(9 mod 3 = 0) and the statement i = i + 1
never executes. So i
never increments beyond 3 and i < x
(3 < 9) is always true.
So the immediate problem is that your code never exits (for x=9
) and you have to stop it, presumably by clicking the close button. But the larger issue is that your logic is bad and your program isn't working the way you think it is.
For example, when x=9
and i=2
, then (x % i) != 0
and that leads to b = b + 1
. That means b > 0
and your program should return 1, which you indicated meant prime in the case of x=7
. But 9 is not prime.
Also, isPrime
has a return type of bool
but you are returning int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With