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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <istream>
#include <string>
using namespace std;
const int NUM_COMPANIES = 6; //rows of salesData
const int NUM_QUARTERS = 4; //columns of salesData
int main()
{
string companyNames[NUM_COMPANIES]; //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
// There should be NO INITIALIZATION of the array in the declaration.
void readData(double[][NUM_QUARTERS], string[], int, int); //a function ReadData that reads data from the file and initializes the arrays
void showData(double[][NUM_QUARTERS], string[], int, int); //The function will print to the screen the contents of the two arrays.
readData(salesData, companyNames, NUM_COMPANIES, NUM_QUARTERS); // call the function readData, and pass in what is needed
showData(salesData, companyNames, NUM_COMPANIES, NUM_QUARTERS);
system("pause");
return 0;
}
void readData(double salesData[][NUM_QUARTERS], string companyNames[], int NUM_COMPANIES, int NUM_QUARTERS) //In Main, call a function WriteData
{
ifstream file("C:\\temp\\data.txt"); // readData will get the information from the Data.txt file
file.open("C:\\temp\\data.txt");
file.get();
for (int row = 0; row < NUM_COMPANIES; row++) // outer loop goes to the next row
{
companyNames[row]= file.get(); // the name of the company is on a line followed by the sales data of the 4 quarters.
for (int col = 0; col < NUM_QUARTERS; col++)// inner loop goes therough the position in the particular row
{
salesData[row][col] = file.get(); //read sales data from the file in order
}
}
file.close(); // close file after completion
}
void showData(double salesData[][NUM_QUARTERS], string companyNames[], int NUM_COMPANIES, int NUM_QUARTERS)
{
for (int comp= 0; comp < 6; comp++) // print company name row of salesData
{
cout << "Company: " << companyNames[comp] << " ";
for (int qtr = 0; qtr < 4; qtr++)
{
cout << " QTR " << qtr+1 << " : " << salesData[comp][qtr]; //print sales data for each quarter
}
cout << endl; // go to next line after each iteration of the loop
}
}
|