home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

prime number problem

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1492
Discussions: 200,987, Posts: 2,379,855, Members: 246,352
Old March 25th, 2007, 10:51 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Mar 2007
Posts: 16
prime number problem

//libraries
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
//function prototypes
void printmessage (ofstream &, int);
void printprimes (ofstream &, int);
bool isprime (int);

//enter the main
int main ()
{
ifstream in;
ofstream out;
//open data file
in.open("in.txt");
//if not open give error message
if (!in)
{
cout << "Did not open in.txt" <<endl;
return 1;
}
//open output file
out.open("out.txt");
//if not give error message
if (!out)
{
cout << "Did not open out.txt" << endl;
return 1;
}
// local variables
int input, n;
//priming read
in >> input;
//start of the loop
while (in)
{
//function
printmessage (out,input);
if (isprime (input))
// EOF
in >> input;
}
cout << "THE END." << endl;
//close the file
in.close();
out.close();


system ("pause");
return 0;
}

//Output
void printmessage (ofstream & out, int input)
{
out <<endl;
out << "The number is " << input;
out <<". The prime numbers less than or equal to " << input << " are: " << endl;
}
void printprimes (ofstream & out, int n)
{
cout << n << " ";
out << n <<" ";
}

bool isprime (int n)
{
int realnum, divisor, count;
ofstream out;
for (realnum=2; realnum <= n; realnum++)
{count = 0;
for (divisor=2; divisor < sqrt(n); divisor++)
{if (realnum % divisor == 0 && realnum != divisor)
{count = 1;
}
}
if (count == 0)
{
printprimes (out, realnum);


}
}
}


now my problem is it wont output to file, but it will to screen. It is probably something really simple, but i have been staring at it for 3 days and can't figure it out. Any help will be appreciated. thanks, i'm a new user
firecookie2k is offline   Reply With Quote
Old March 26th, 2007, 01:50 PM     #2 (permalink)
Junior Member
 
Join Date: Mar 2007
Posts: 16
Smile
wont print prime number to file, but will to screen

Maybe you guys can help me with this C++ program. I wrote all of the code and just need a little help to finish. It is due by Wednesday. I can get the cod ebelow to print to screen using cout, but cannot get to print to a notepad file called out.txt. It opens the file and also prints out the printmessage to file, but does not print out printprimes. Any help will be greatly appreciated. Sorry this is the second time i posted, but I guess the first one wasn't detail enough.

//libraries
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
//function prototypes
void printmessage (ofstream &, int);
void printprimes (ofstream &, int);
bool isprime (int);

//enter the main
int main ()
{
ifstream in;
ofstream out;
//open data file
in.open("in.txt");
//if not open give error message
if (!in)
{
cout << "Did not open in.txt" <<endl;
return 1;
}
//open output file
out.open("out.txt");
//if not give error message
if (!out)
{
cout << "Did not open out.txt" << endl;
return 1;
}
// local variables
int input, n;
//priming read
in >> input;
//start of the loop
while (in)
{
//function
printmessage (out,input);
if (isprime (input))
// EOF
in >> input;
}
cout << "THE END." << endl;
//close the file
in.close();
out.close();


system ("pause");
return 0;
}

//Output
void printmessage (ofstream & out, int input)
{
out <<endl;
out << "The number is " << input;
out <<". The prime numbers less than or equal to " << input << " are: " << endl;
}
void printprimes (ofstream & out, int n)
{
cout << n << " ";
out << n <<" ";
}

bool isprime (int n)
{
int realnum, divisor, count;
ofstream out;
for (realnum=2; realnum <= n; realnum++)
{count = 0;
for (divisor=2; divisor < sqrt(n); divisor++)
{if (realnum % divisor == 0 && realnum != divisor)
{count = 1;
}
}
if (count == 0)
{
printprimes (out, realnum);


}
}
}
firecookie2k is offline   Reply With Quote
Old March 26th, 2007, 02:06 PM     #3 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,242
Send a message via AIM to Rootstonian
I'm surprised you're not getting an error on isprime; it should be expecting a RETURN value and I don't see one.

You're aslo declaring 2 ofstream out. The one in isprime is bogus.

Maybe you need to return a value from isprime and "do something" based on that
Rootstonian is offline   Reply With Quote
Old March 26th, 2007, 07:16 PM     #4 (permalink)
Junior Member
 
Join Date: Mar 2007
Posts: 16
Smile
still not working

when i return a value from the bool function the only output is
4436984 three times. i have no idea where that value is coming from. if i put printprimes as a function after if (isprime), it just prints out all of the numbers up to the input.
firecookie2k is offline   Reply With Quote
Old March 26th, 2007, 07:22 PM     #5 (permalink)
Member
 
sulusulu's Avatar
 
Join Date: Oct 2001
Location: Wisconsin
Posts: 202
Have you tried going through your program with a calculator and pencil? I have found that this procedure usually points out any errors.
__________________
Accept that some days you're the pigeon, and some days you're the statue. -- Scott Adams
sulusulu is offline   Reply With Quote
Old March 26th, 2007, 07:41 PM     #6 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,242
Send a message via AIM to Rootstonian
According to your function prototype, isprime is to return a BOOL; 0 or 1 or TRUE or FALSE. I see no return 0; return 1; return TRUE or return FALSE in that function.

I'll psuedocode this for you...you're close, but I'm not going to "give" it to you

Open Files
While not EOF Input file
Read Input File
Is this number Prime?
Yes...print to screen....print to file
No...do nothing
back to start of while loop
Out of while loop
Close Files
End Program

I'm not trying to be an a$$, but it really is this easy. Start a new source file and cut and paste from your original. I've done that a thousand times when I get stuck with a program; sometimes you spend too much time trying to fix what you first wrote. Doing it over from scratch gives you a fresh start (and 9 times out of 10 a new and correct view of the program).

Last edited by Rootstonian : March 26th, 2007 at 07:45 PM.
Rootstonian is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
prime voltages italiamodena575 Technical Support 2 December 20th, 2004 01:57 AM
Counter Striker Key number! Problem leosmdp General Gaming Discussion 5 August 2nd, 2004 11:58 AM
Prime 95 found a problem... MitaDC General Tech Discussion 8 June 1st, 2004 10:58 AM
GIMPS finds the largest prime number Virus Distributed Computing 0 December 5th, 2001 11:33 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3063)
Charges against non-tippers dropped.. (20)
Health Care Rationing (11)
Delete an OS (17)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
windows vista security holes (19)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (7)
windows 7 problem (7)
Internet Lost (5)
[F@H SPAM 11/16/09] ! 1/2 months to.. (39)
Recent Discussions
How to convert Mod/Tod video to AVI/M.. (0)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (39)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
Desktop Calendar Application (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
Point and Shoot Camera Suggestions. (7)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
Help getting around port 80 for camer.. (5)


All times are GMT -4. The time now is 04:34 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28