modifying my program
Nov 3, 2019 at 10:08pm
I need help modifying my code so that it prompts the user for the file and stores the data there instead
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include <fstream>
using namespace std;
bool isPrime(int);
int main()
{
int num=0;
int i;
bool prime;
ofstream outFile;
outFile.open("PrimeList.txt");
while (num == 0 )
{
cin >> num;
}
for(i=2;i<num;i++)
if(isPrime(i))
outFile << i << "\n";
cout << "Prime numbers written to PrimeList.txt.\n";
outFile.close();
return 0;
}
bool isPrime(int n)
{
int i;
for(i=2;i<n-1;i++)
if(n%i==0)
return false;
return true;
}
|
Nov 3, 2019 at 10:13pm
You can read numbers from user. How is reading a filename different?
Nov 3, 2019 at 10:15pm
Use strings
Make sure you
#include <string>
1 2 3
|
string filename;
cin >> filename;
ofstream outFile(filename); // same as .open(filename)
|
Topic archived. No new replies allowed.