Apr 21, 2013 at 9:09pm UTC
Good afternoon all,
My program asks the user for a file, reads the file, creates a new file, generates 5 additional numbers ,adds these numbers plus the old numbers to the new file. I would like to know how to show the random number seed. I have left it blank where I would like it to show the seed.
Thank you for your time
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void sortArray( int ary[], int size)
{
for ( int i = 0;i < size;i++)
for ( int j = 1; j < size - i;j++)
if (ary[j-1] > ary[j])
{
int temp = ary[j-1];
ary[j-1] = ary[j];
ary[j] = temp;
}
}
int main()
{
string ifilename, ofilename, line;
ifstream inFile, checkOutFile;
ofstream outFile;
char response;
int ary [10];
cout << "Please enter the name of the file you wish to open : " ;
cin >> ifilename;
inFile.open(ifilename.c_str());
if (inFile.fail())
{
cout << "The file " << ifilename << " was not successfully opened." << endl;
cout << "Please check the path and name of the file. " << endl;
exit(1);
}
else
{
cout << "The file is successfully opened." << endl;
}
cout << "Please enter the name of the file you wish to write : " ;
cin >> ofilename;
checkOutFile.open(ofilename.c_str());
if (!checkOutFile.fail())
{
cout << "A file " << ofilename << " exists.\nDo you want to continue and overwrite it? (y/n) : " ;
cin >> response;
if (tolower(response) == 'n' )
{
cout << "The existing file will not be overwritten. " << endl;
exit(1);
}
}
outFile.open(ofilename.c_str());
if (outFile.fail())
{
cout << "The file " << ofilename << " was not successfully opened." << endl;
cout << "Please check the path and name of the file. " << endl;
exit(1);
}
else
{
cout << "The file is successfully opened." << endl;
}
cout << "The original list : " << endl;
outFile << "The original list : " << endl;
for ( int i = 0; i < 5;i++){
inFile >> ary[i];
outFile << ary[i] << "\n" ;
cout << ary[i] << endl;
}
cout << "The random number seed :" << endl;
outFile << "The random number seed :" << endl;
cout << "The generated numbers : " << endl;
outFile << "The generated numbers : " << endl;
for ( int i = 0;i < 5;i++)
{
ary[i+5] = rand() % 1000;
outFile << ary[i+5] << "\n" ;
cout << ary[i+5] << endl;
}
cout << endl;
srand(14792);
sortArray(ary,10);
cout << "The merged list : " << endl;
outFile << "The merged list : " << endl;
for ( int i = 0; i < 10; i++)
{
cout << ary[i] << endl;
outFile << ary[i] << "\n" ;
}
while (getline(inFile, line))
{
cout << line << endl;
outFile << line << endl;
}
inFile.close();
outFile.close();
int test;
cin >> test;
}
Apr 21, 2013 at 9:15pm UTC
You can seed it with whatever you want. You probably want to do that before calling rand().
In your program, you invoke srand(14792). So you could say, int seed = 14792; srand(seed); cout << seed;
Apr 21, 2013 at 11:43pm UTC
I see thank you for the reply