Quote:
Originally Posted by gmann145
so this really got me i, i have to write the numbers to an array then to the file, all i keep getting is numbers 0-99 however in the file help would be greatly appreciated.
#include <iostream> #include <fstream> using namespace std; bool isPrime(int c); void write_array(int x, ofsteam &outfile); void write_prime(int num, int p); int main() { int p = 0; int count; const int arrays = 100 ofstream pfile("Prime.txt"); //Declare variables for (int c =0; c<100,c++) //Write all the arrays to 0 arrays[c]=0; for (int x = 2; x < 100; x++) //Finding The Prime Numbers { if(isPrime(x)) { write_array(x, pos); //Writing Primes to Array pos++ } } for(count = 0; arrays[count] != 0; count ++) //Write arrays to file write_prime(arrays[count],pfile); pfile.close(); return 0; } bool isPrime(int c) { for (int y = 2; y < c; y++) { if(c % y == 0) { return false; break; } } return true; } void write_prime(int p, ofstream &pfile) { pfile << p << endl; } void write_array(int num, int p) { arrays[p]=num; return; }
|
Wow, where to start?
arrays is a const int (with no semicolon), but later you try to use it as an array. Also your
for loop has a comma instead of a semicolon.
p is apparently trying to be
pos. Your functions are too simple to be functions (you make it more confusing than helpful).
for (int c =0; c<100;c++) //Write all the arrays to 0
arrays[c]=0;
This can be eliminated by just using
pos as a limit in the last for loop.
I think you should go over a tutorial from the beginning and rethink your logic here, everything is more complicated than it needs to be.