Thread: Problem running prime number test.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    11

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    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. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    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".
    Last edited by galactic_ronin; 02-20-2008 at 05:46 AM.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    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. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abk View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by galactic_ronin View Post
    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.
    Last edited by abh!shek; 02-20-2008 at 05:59 AM.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    UGH...
    It's still not working for me...

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by galactic_ronin View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Shouldn't it be Windows Console Application and not CRL Console Application
    Also note that standard C++ header is not <math.h> but <cmath>
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    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. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by galactic_ronin View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by galactic_ronin View Post
    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.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    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
    Enable Large Adresses Default
    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. #15
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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.
    Last edited by dudeomanodude; 02-20-2008 at 06:46 AM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why Errors in Prime Number Loop
    By wco5002 in forum C++ Programming
    Replies: 15
    Last Post: 03-22-2008, 10:49 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Prime Number problem
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 08:00 PM