// asterisksRectangle2.cpp
// Generates a text file containing a rectangle of asterisks.
// Demonstrates nested loops.
#include <iostream>
#include <fstream>
int main()
{
// Announce purpose of program:
char filename[] = "rectangle.txt";
cout << "This program will print a rectangle of asterisks" << endl
<< "to text file " << filename << "." << endl;
// Prepare to write to file:
ofstream outputFile;
outputFile.open(filename);
if ( ! outputFile )
{
cout << "Cannot write to file " << filename << "." << endl;
return 1;
} // if
// Prompt user for dimensions:
cout << "Enter number of rows:>";
int numberOfRows;
cin >> numberOfRows;
cout << "Enter number of columns:>";
int numberOfColumns;
cin >> numberOfColumns;
// Print rectangle of asterisks to file:
for ( int row = 0; row < numberOfRows; row++ )
{
for ( int column = 0; column < numberOfColumns; column++ )
outputFile << " *";
outputFile << endl;
} // for row
// Check error state of output file after last output:
if ( outputFile )
cout << "File " << filename
<< " has been created successfully." << endl;
else
cout << "Error writing to file " << filename << "." << endl;
return 0;
} // function main