File IO writing and reading

This programs description is to write a program that generates random product sku numbers and prices, stores them in arrays and writes them out to a text file. The program then will read the product records into a second set of arrays, compute the average product price and display it to the screen.
The main, function prototype, and sku function were given. My program successfully compiles but displays no output. I think this is because my files failed to connect. I cannot find my issues. Any help is greatly appreciated

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

/*----------------------------------------------------------------------------------------*/
// Constants to be used.
const int ARRAY_SIZE = 100;

const string OUTPUT_FILE = "products.txt";
const string INPUT_FILE = "products.txt";
/*----------------------------------------------------------------------------------------*/
// Attempts to connect the filename to the file handle. Returns true if successful,
// false otherwise.

bool connectOutputFile(ofstream& fout, const string& filename);

// Attempts to connect the filename to the file handle. Returns true if successful,
// false otherwise.

bool connectInputFile(ifstream& fin, const string& filename);
/*----------------------------------------------------------------------------------------*/
// Generates length random sku numbers. Each sku number has the format AA99A, where
// A is an alpha character (A-Z) and 9 a digit (0-9).

void generateSKUNumbers(string sku[], const int length);

// Generates length random prices. Each price is in the range of 0.01 to 10.99.

void generateProductPrices(double price[], const int length);
/*----------------------------------------------------------------------------------------*/
// Writes the sku and price of each product to the output file, one record per line.
// A record is basically the sku and price, comma delimited, e.g. sku,price
//
// The function must first connect to the stream, check it and if opened, then
// write to it.

void writeProductsToFile(const string& filename, const string sku[],
const double price[], const int length);

// Reads the sku and price of each record from the file and stores them into the arrays. The
// arrays are assumed to be large enough to contain all the records.
//
// The function must first connect to the stream, check it and if opened, then
// read from it.

void readProductsFromFile(const string& filename, string sku[], double price[]);
/*----------------------------------------------------------------------------------------*/
// Computes the average product price. This average is returned.
double averageProductPrice(const double price[], const int length);

// Displays to the screen the average product price.
void displayAverageProductPrice(const string& label, const double averageProductPriceInDollars);
/*----------------------------------------------------------------------------------------*/



int main() {
string skuGenerated[ARRAY_SIZE];
double priceGenerated[ARRAY_SIZE];

/* Generate the product sku and price. */
generateSKUNumbers(skuGenerated, ARRAY_SIZE);
generateProductPrices(priceGenerated, ARRAY_SIZE);

/* Write the products to the output file. */
writeProductsToFile(OUTPUT_FILE, skuGenerated, priceGenerated, ARRAY_SIZE);

string skuReadIn[ARRAY_SIZE];
double priceReadIn[ARRAY_SIZE];

/* Read the products from the file. */
readProductsFromFile(INPUT_FILE, skuReadIn, priceReadIn);

/* Compute the average product price. */
double averageProductPriceInDollars = averageProductPrice(priceReadIn, ARRAY_SIZE);

/* Display the average price to the screen. */
displayAverageProductPrice("\nThe average product price is $", averageProductPriceInDollars);

cout << endl << endl;
return 0;
}// end main()
/*----------------------------------------------------------------------------------------*/

bool connectOutputFile(ofstream& fout, const string& filename) {
fout.open(filename.c_str());

return fout.is_open();
}

bool connectInputFile(ifstream& fin, const string& filename) {
fin.open(filename.c_str());

return fin.is_open();
}

/* Generates length random sku numbers. Each sku number has the format AA99A, where
* A is an alpha character (A-Z) and 9 a digit (0-9).
*/
void generateSKUNumbers(string sku[], const int length) {
string letters[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
"P","Q","R","S","T","U","V","W","X","Y","Z"};
string digits[] = {"0","1","2","3","4","5","6","7","8","9"};


/* Generate the random number in the proper range (0-25 for alpha, 0-9 for digits)
* and use as the index into the string arrays to get to the actual string values.
*/
for (int i = 0; i <= length - 1; i++) {
sku[i] = letters[rand() % 26] + letters[rand() % 26] +
digits[rand() % 10] + digits[rand() % 10] +
letters[rand() % 26];
}

}

void generateProductPrices(double price[], const int length) {
for (int i = 0; i <= length - 1; i++) {
price[i] = (double)(rand()%1099+1)/100;
}
}

void writeProductsToFile(const string& filename, const string sku[], const double price[], const int length) {
ofstream foutProducts;

if ( !connectOutputFile(foutProducts, filename) ) {
cerr << "Error opening to file " << filename << " for writing. Aborting." << endl;
exit(1);
}

for (int i = 0; i <= length - 1; i++) {
foutProducts << sku[length] << price[length];
foutProducts << " ";
}

foutProducts.close();

cout << filename << "has been successfully created.";
}

void readProductsFromFile(const string& filename, string sku[], double price[]) {
ofstream foutProducts;
ifstream finProducts;

if ( !connectOutputFile( foutProducts, filename)) {
cerr << "Error opening file " << filename << " for writing. Aborting." << endl;
exit(1);
}

if (!connectInputFile( finProducts, filename)) {
cerr << "Error opening file " << filename << " for reading. Aborting.";
}


foutProducts.close();
finProducts.close();

cout << filename << "has been successfully read.";
}


double averageProductPrice(const double price[], const int length) {
double sumOfPrices = 0;

for (int i = 0; i <= length - 1; i++) {
sumOfPrices += price[i];
}

double averageProductPriceinDollars = (length != 0)? (double) sumOfPrices / (double) length : 0.0;

return averageProductPriceinDollars;
}

void displayAverageProductPrice(const string& label, const double averageProductPriceInDollars) {
cout << label << averageProductPriceInDollars;

}
Topic archived. No new replies allowed.