Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 14th, 2008, 11:01 AM   #1
gmann145
Newbie
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0 gmann145 is on a distinguished road
finding prime using bool

Hey I'm trying to figure out how to fix this program, i need to create and write a file Prime.txt and store prime numbers 1-100 using bool. right now I'm stuck and not sure whats wrong. All I'm getting is the black screen, and I'm not sure exactly where the file is then stored


c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. bool isPrime(int numcheck);
  6. void write_prime(int prime, ofstream &outfile);
  7.  
  8. int main()
  9. {
  10. ofstream primefile("prime.txt");
  11.  
  12. for (int ntc = 2; ntc = 100; ntc++)
  13. {
  14. if(isPrime(ntc))
  15. write_prime(ntc, primefile);
  16. }
  17. primefile.close();
  18. return 0;
  19. }
  20.  
  21. bool isPrime(int numcheck)
  22. {
  23. for (int x = 2; x < numcheck; x++)
  24. {
  25. if(numcheck % x == 0)
  26. {
  27. return false;
  28. break;
  29. }
  30.  
  31. }
  32. return true;
  33. }
  34.  
  35. void write_prime(int prime, ofstream &primefile)
  36. {
  37. primefile << prime << "\n";
  38.  
  39. }
gmann145 is offline   Reply With Quote
Old Feb 14th, 2008, 1:38 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 489
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: finding prime using bool

line 12: replace ntc = 100 with ntc < 100
Ancient Dragon is offline   Reply With Quote
Old Feb 14th, 2008, 1:39 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: finding prime using bool

I believe that the file will be written to the directory in which the executable resides. If you are using an IDE which takes care of compilation for you, you may have to dig around to find it.
titaniumdecoy is offline   Reply With Quote
Old Feb 14th, 2008, 1:43 PM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 489
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: finding prime using bool

The problem was not the file but that infinite loop on line 12. The program never was able to write anything to the file.
Ancient Dragon is offline   Reply With Quote
Old Feb 14th, 2008, 2:26 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: finding prime using bool

My bad. I have had a problem in the past with not being able to locate the output of C++ programs that I wrote.
titaniumdecoy is offline   Reply With Quote
Old Feb 14th, 2008, 5:19 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,788
Rep Power: 5 Sane will become famous soon enough
Re: finding prime using bool

Just as an aside, if you're generating a list of primes (as opposed to determining whether or not an arbitrary number is prime), the Sieve of Eratosthenes is a fairly intuitive and simple algorithm for doing just that. It's easy to implement, and might be a fun programming excercise for you. It's by no means necessary for the requirements of the program, but it will surely impress your teacher (if this is an assignment).
Sane is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
prime number, problem cairo C++ 3 Jun 2nd, 2006 1:43 PM
finding primes in an array victor_shade Java 4 Oct 27th, 2005 5:48 PM
Defining a bool type nnxion C 6 Oct 7th, 2005 8:39 AM
Prime Numbers Konnor C++ 6 Sep 12th, 2005 6:46 PM
Help with a couple prime numbers programs!! BehemothPhoenix C++ 3 Mar 20th, 2005 2:06 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC