I have to take two arrays, put them in a function, and then put them into a file. I have no problem actually making the arrays, but I am not quite sure how to go about defining the function for both arrays.
I appreciate any help!

#include "stdafx.h"
using namespace std;

void ShowData(string[], double[][4], const int[]);

const int SIZE = 6;
const int NUM_ROWS=6;
const int NUM_COLS=4;
int main()

string  companies[SIZE] = {"SDF","VXC","CCL","REL","MPL","GHB"};
double sales [NUM_ROWS] [NUM_COLS] =
{{10000, 70000, 60000, 80000},
{35000, 55000, 34000, 60000},
{35000, 46000, 17000, 29000},
{29000, 30000, 31000, 38000},
{42000, 44000, 46000, 45000},
{20000, 23000, 35000, 43000}};

ofstream outFile;


outFile << writeData(companies,sales);


return 0;
2 Years
Discussion Span
Last Post by (Z!Z!)

It looks like you have 6 companies ...

And sales for 4 periods for each company ?

This lends itself to a (data) struct
using ONE array of struct (or better a C++ vector of struct)

(NOT 2 arrays ... that is 'old-school'!)

struct MyBus
   string name;
   double[4] sales;
} ;

where you could use a file data structure like this

sales1 sales2 sales3 sales4 company name till end
next lines ... as above ...

to write the data for each company all on one line

Then ... the read back is easy ... just read the lines and parse each line using stringstream ...to fill each struct

your file structure might begin with a line holding the number of companies to be read

then followed by a line holding the number of sales periods for each company

then all the data lines would follow.

Edited by David W: fixed typo


Hey, I appreciate your response and definitely wish I could do it that way, but I am required to use two different arrays.


For the problem you have, your output function needs to be set up like your ShowData().

You'll have to decide how you want to write the data to the file. You could write all the names, then all the data, by rows.

Or you could write a name, then all its sales date, new line, name, sales....

Just be sure you set your reading function to properly align the data when read back.


This is the start of the function, now all you have to do is create two nested loops. The outermost loop iterated from 0 to 6 which displays the compainies, and the inner loop counts from 0 to 4, displaying each of the sales on the same line as the company name. Each of the columns should be separated with a space, tab, comma, or some other separator of your choice.

ofstream& writeData(ofstream& out, char companies[6],char sales[6][4])

   return out;

There is a second part where I have to then read from the file, and then create a loop where I can search a company's name and then get the corresponding amount of money based on the quarter. ie company "SDF" q 1 would be 10000. But I am more worried about figuring this first part out.


Here is a common 'show data' example ... that may help you to get started ... ( Oops ... looks like I missed the post just now of Dani's 'Ancient Dragon' :)

// twoArrays.cpp //

#include <iostream>
#include <iomanip> // re. setw( .. )
//3include <fstream>
#include <string>

using namespace std;

const int NUM_COMPANIES = 6;

const int NUM_COLS = 4;

void print( const string[], const int[][NUM_COLS], int size );

int main()
    string companies[] = {"SDF","VXC","CCL","REL","MPL","GHB"};

    int sales [][NUM_COLS] =
        {10000, 70000, 60000, 80000},
        {35000, 55000, 34000, 60000},
        {35000, 46000, 17000, 29000},
        {29000, 30000, 31000, 38000},
        {42000, 44000, 46000, 45000},
        {20000, 23000, 35000, 43000}

    cout << setw(10) << "Company"
         << setw(10) << "sales1"
         << setw(10) << "sales2"
         << setw(10) << "sales3"
         << setw(10) << "sales4" << endl;

    print( companies, sales, NUM_COMPANIES );


void print( const string co[], const int sales[][NUM_COLS], int size )
    for( int i = 0; i < size; ++i )
        cout << setw(10) << co[i];
        for( int j = 0; j < NUM_COLS; ++j )
            cout << setw(10) << sales[i][j];

        cout << endl;

Edited by David W: Added comment


Thanks guys, I think I got this thing figured out now

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.