I have wrote a function to check to see if a number 1 -200 is prime. However, I am trying to write it to a file but I am having trouble figuring out how to do it. can someone help steer me in the right direction? Thanks
#include <iostream>
#include <fstream>
using namespace std;
//function prototype checks if number is prime
bool isPrime(int);
int main()
{
int number;
cout << "Enter a number to see if it's prime: ";
cin >> number;
for(int num=0; num <=200;)
{
if(isPrime(number))
cout << number << " is a Prime number." << endl;
else
cout << number <<" is not a prime number." << endl;
break;
}
}
//function definition
bool isPrime(int num)
{
int count = 0;
for(int i = 2; i <= num/2; i++)
{
if(num % i == 0)
count++;
}
You have the correct header file included. You have to declare a variable to the file data type. Then open the file you want to write to. Then in your if statement use an insertion operator to write to the file. Remember to close the file after you are done with it.
1 2 3 4 5 6 7 8
ifstream variableName;
variableName.open("FileNameHere.txt")
if(isPrime(number)){
cout << number << " is a Prime number." << endl;
variableName << number;
variableName.close();
}
You could optimize your function a bit as well. Like delete the variable count completely and place the return false; in your if statement for when it is true. Because you don't have to go further once it is it is true because it is for sure not prime then. You could get it down to one for loop an if statement and two return statements. Like this.
1 2 3 4 5 6 7 8
bool isPrime(int num)
{
for(int i =2; i <= num/2 ; i++){
if(num % i ==0)
returnfalse;
}
returntrue;
}
OK guys thanks that really helped me I have it writing to the file but now I have another problem I need to write all the numbers to the file instead of just one but I can only get it to print the number 1 to the file. Where am I messing up Heres my new code:
#include <iostream>
#include <fstream>
using namespace std;
//function prototype checks if number is prime
bool isPrime(int);
You're calling primenum.close() inside the for loop in main. Because of this, it is writing one number to the file and then cannot write anymore (because no file is open). Try moving that to the end of the main function.
This is the output I am getting when I changed it up but its only writing 200 is a prime num now and 200 isnt even a prime number lol
1 is a Prime number.
2 is a Prime number.
3 is a Prime number.
5 is a Prime number.
7 is a Prime number.
11 is a Prime number.
13 is a Prime number.
17 is a Prime number.
19 is a Prime number.
23 is a Prime number.
29 is a Prime number.
31 is a Prime number.
37 is a Prime number.
41 is a Prime number.
43 is a Prime number.
47 is a Prime number.
53 is a Prime number.
59 is a Prime number.
61 is a Prime number.
67 is a Prime number.
71 is a Prime number.
73 is a Prime number.
79 is a Prime number.
83 is a Prime number.
89 is a Prime number.
97 is a Prime number.
101 is a Prime number.
103 is a Prime number.
107 is a Prime number.
109 is a Prime number.
113 is a Prime number.
127 is a Prime number.
131 is a Prime number.
137 is a Prime number.
139 is a Prime number.
149 is a Prime number.
151 is a Prime number.
157 is a Prime number.
163 is a Prime number.
167 is a Prime number.
173 is a Prime number.
179 is a Prime number.
181 is a Prime number.
191 is a Prime number.
193 is a Prime number.
197 is a Prime number.
199 is a Prime number.
Press any key to continue . . .