For this program the main function was provided and is shown below, also implementing the functions given and also shown below. I am having difficulties implementing the last two functions: the function
void writeRandomNumbersToFile(const string& filename)
is instructed to: " Generate random numbers, the range is -1000 to 1000. You will need a total of 1000 numbers. Use the statement rand() % 2001 - 1000 to generate such numbers" the numbers must be written to a text file. Then in the next function, the positive and negative numbers must be divided into a separate text file for positive and negative numbers. I'm not experienced with file IO and have been having a really hard time figuring out how to finish the program using the last two functions. Thanks for the help in advance!
#include <iostream>
#include <fstream>
using namespace std;
bool connectToInputFile(ifstream& fin, const string& filename);
// This function attempts to make the connection between the input file handle and the filename.
// It returns true on success, false on failure to connect.
bool connectToOuputFile(ofstream& fout, const string& filename);
// This function attempts to make the connection between the output file handle and the filename. It
// returns true on success, false on failure to connect.
void writeRandomNumbersToFile(const string& filename);
// This function first attempts to connect to the filename and exits the program if not successful. Otherwise it
//generates 1000 numbers in the range -1000 to 1000. Make sure you write the 1000 numbers to the file, 20 numbers per line.
void splitNumbersBySign(const string& inputFilename, const string& posFilename, const string& negFilename);
// This function first makes the three connections to the three supplied filenames. If any connection fails, then the program exits.
// Otherwise it reads from the input file each of the 1000 numbers, and if the number is negative writes it to the negFilename file, else writes it to the posFilename file.
int main() {
/* Generate the 1000 random numbers in the range -1000 to 1000 and save
* them in a file.
*/
writeRandomNumbersToFile("randomNumbers.txt");
/* Split the random numbers into two files. One containing all the positive
* numbers and the other all the negative numbers.
*/
splitNumbersBySign("randomNumbers.txt", "pos.txt", "neg.txt");
cout << "Both pos.txt and neg.txt have been created successfully." << endl;
Make sure you have some place to store the numbers.
You know you will need 1000 numbers, so you could use an array of size 1000 to hold them all.
1 2 3 4 5 6 7 8 9 10 11
writeRandomNumbersToFile(const string& filename, /** argument here for the thing holding the numbers */ )
{
//create a ofstream object, and write the numbers to the file with a delimiter between the values (a space, tab, new-line, etc)
}
void splotNumbersBySign(const string& inputFilename, cost string& posFileName, const string&negFilename)
{
//create TWO fstream objects, and one ifstream object
//read in the numbers and send the positive ones to posFileName, and the negative ones to negFilename
}
Note: Remember to close your streams (i.e. "fin.close(), fout.close()" etc).