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
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int NUM_COMPANIES = 6; //rows
const int NUM_QUARTERS = 4; //columns
int main()
{
void writeData(double[][NUM_QUARTERS], string[], int, int); //function prototype
string companyNames[NUM_COMPANIES] = { "ABC", "DEF", "GHI", "JKL", "MNO", "GCP" }; //declare a one dimensional string array to hold the names of 6 different companies
double salesData[NUM_COMPANIES][NUM_QUARTERS] = //a two dimensional double array to hold the sales data for the four quarters in a year
{ { 40000, 50000, 60000, 70000 },
{ 35000, 45000, 55000, 65000 },
{ 25000, 26000, 27000, 28000 },
{ 31000, 32000, 33000, 34000 },
{ 42000, 43000, 44000, 45000 },
{ 10000, 20000, 30000, 40000 } };
writeData(salesData, companyNames, NUM_COMPANIES, NUM_QUARTERS); // call the function writeData, and pass in what is needed
system("pause");
return 0;
}
void writeData(double salesData[][NUM_COMPANIES], string companyNames, int NUM_COMPANIES, int NUM_QUARTERS){ //In Main, call a function WriteData
ofstream file("C:\\temp\\data.txt"); // WriteData will create a simple text file - Data.txt file
for (int row = 0; row < 6; row++) // outer loop goes to the next row
{
file << companyNames[row]; // the name of the company is on a line followed by the sales data of the 4 quarters.
for (int col=0; col < 4; col++)// inner loop goes therough the position in the particular row
{
file << salesData[row][col] << " "; //write the sales data to the file in order
}
file << endl; // after each row is finished, moves to the next line
}
file.close(); // close file after completion
}
|