# Thread: Problem running prime number test.

1. ## Problem running prime number test.

Code:
```#include <stdafx.h>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int n;  // Number to test for prime-ness
int i; // Loop counter
int is_prime; // Boolean flag

//Assume that a number is prime until proven
//otherwise

is_prime = true;

//Get a number from the keyboard.

cout << "Enter a number and press ENTER: ";
cin >> n;

//Test for prime-ness by checking for divisibility
//by all whole numbers from 2 to sqrt(n).

i = 2;
while (i <= sqrt(static_cast<double>(n)))
{
if (n % i == 0)
is_prime = false;
i++;
}

//Print results

if (is_prime)
cout << "Number is prime.";
else
cout << "Number is not prime.";

return 0;
}```
This program was written in Visual C++ Express on a CLR Console Application template. The problem that I'm having is that the program won't run when I hit Ctrl+F5 and I get an error message that says 'There were building errors'. My guess is that the <math.h> library might not be included in the express edition or perhaps I haven't installed it or something. But I wanted to post it here just in case I was missing any obvious mistake that I'm not seeing.

NOTE: This is a noob programmer speaking. Been writing code for about 4 days total so keep that in mind.

2. If you use "F7" to just build the project, you should get a "Todo" list of errors, and if you click on one of them, it will show the line of code it refers to - if you don't understand a particular one, please post the error and the corresponding line that it is [with, say, 4 lines before and after, as errors sometimes reflect the code BEFORE the error].

As to math.h - you could try <cmath> instead - it's the same thing, but set up for C++.

--
Mats

3. Is it proven that if a number is not divisible by numbers upto sqrt(n), then it won't be divisible by any number greater than sqrt(n) ?? (possible, IDK)

When I made it, I checked upto n-1

EDIT:
And reason for casting it to double ?

4. When I hit F7 I just get a line of text at the output column. It prints "build started..." then instantly replaces the text with "build failed".

5. EXTRA NOTE: I copied this code (4 times) directly from chapter 2 of the C++ Without Fear book so I'm 99&#37; sure the code is correct.

6. Originally Posted by abk
Is it proven that if a number is not divisible by numbers upto sqrt(n), then it won't be divisible by any number greater than sqrt(n) ?? (possible, IDK)

When I made it, I checked upto n-1

EDIT:
And reason for casting it to double ?
Yes, if you think about it, any number can not be produced where BOTH sides of the number is greater than the square-root of the number, e.g. 6 = 2 * 3, 2 < sqrt(6) [approx 2.4], 25 = 5 * 5, 5 == sqrt(25), 18 = 2 * 9, 2 < sqrt(18) [approx 4.2].

Although, integer math may get in the way, so sqrt(n)+1 may be necessary as the limit - the above code is slightly suboptimal if the compiler doesn't realize that sqrt(n) is constant for the loop, and calculates sqrt every time - it would be better to have a "int limit = sqrt(n)+1" and then "while(i <= limit) ...";

Casting to double is to make "sqrt()" happy - it is a "double" function [at least if you don't find a C++ template version somewhere else]. I didn't include casts in the above code to make it simpler to read.

--
Mats

7. Originally Posted by galactic_ronin
EXTRA NOTE: I copied this code (4 times) directly from chapter 2 of the C++ Without Fear book so I'm 99&#37; sure the code is correct.
Its working for me (VC++ 2008). No problems building or running, and its giving the correct results.

8. UGH...
It's still not working for me...

9. Originally Posted by galactic_ronin
When I hit F7 I just get a line of text at the output column. It prints "build started..." then instantly replaces the text with "build failed".
There should be some sort of error indicating WHY build failed, but I suspect that you are not compiling for a "Console" app - Go to Project->properties->Linker->System and set "Subsystem type" to Console [I'm assuming it's Windows], click OK on the properties box.

The code runs fine on my machine when I compile it with gcc [except I had to remove the stdafx.h include].

--
Mats

10. Shouldn't it be Windows Console Application and not CRL Console Application
Also note that standard C++ header is not <math.h> but <cmath>

11. here should be some sort of error indicating WHY build failed, but I suspect that you are not compiling for a "Console" app - Go to Project->properties->Linker->System and set "Subsystem type" to Console [I'm assuming it's Windows], click OK on the properties box.
I went ahead and made these changes and I'm still getting the same error. Ill also note that I am using windows and that the book I'm using is telling me to use <math.h> so I have yet to hear of this '<cmath>'. (Though I have tried both and neither will work.)

12. Originally Posted by galactic_ronin
I went ahead and made these changes and I'm still getting the same error. Ill also note that I am using windows and that the book I'm using is telling me to use <math.h> so I have yet to hear of this '<cmath>'. (Though I have tried both and neither will work.)
So, if I get your right, you did have "Windows" in the subsystem setting, and you have now changed it to console, and it's still not working?

Can you post what came out on the console window in Visual Studio? The last 10-20 lines [with code-tags] would be useful.

--
Mats

13. Originally Posted by galactic_ronin
I went ahead and made these changes and I'm still getting the same error. Ill also note that I am using windows and that the book I'm using is telling me to use <math.h> so I have yet to hear of this '<cmath>'. (Though I have tried both and neither will work.)
I have the same book, and it's a little outdated (hence the <math.h>). Yes you should be using <cmath> instead, don't worry the same functions are there, just more up to date and for C++ and not C (a lot of this book has old C-Style stuff).

I'm still a bit perplexed about the static_cast myself. The book comes with an old version of the Borland compiler I think, so maybe he (the author) is just trying to avoid compiler warnings?

I still think an IDE tutorial is in order, this is the second post of someone following this book and getting real stuck using MSVS++ (although the two should be unrelated) in the past couple days.

14. Sorry for the confusion, no I did not have anything in the subsetting system ie it said "Not Set". I thought you were asking if I was using windows as an operating system (remember I'm still a noob at this). When I changed it to 'Console (/SUBSYSTEM:CONSOLE)' I got these results.

Heap Reserve Size 0
Heap Commit Size 0
Stack Reserve Size 0
Stack Commit Size 0
Terminal Server Default
Swap Run from CD No
Swap Run from Network No
Driver Not Set

These settings didn't change when I switched the subsetteting. They were always like this.

15. Your subsystem settings shouldn't matter here. I suspect the problem lies elsewhere. So set those back to default. Now I would start simply by creating an "empty" project (if that's still an option in VS), paste the code in, and let her rip.

What I mean is matsp was saying that your subsystem should be windows, I don't think he was explicitly telling you to change that. By "console" he meant when you choose your project type, choose a "console app."

Either console app or empty project should work with no problems.