/*
File: Rainfall.cpp
Description: Write a program that reads in the average monthly rainfall for a city for each month
of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program
then prints out a nicely formnatted table showing the rainfall for each of the previous 12 months as well
as how much below or above average the rainfall was for each month. The average monthly rainfall is given
for months January-December in order. To obtain the actual rainfall for the previous 12 months, the program first
asks what the current month is and then asks for the rainfall figures for the previous 12 months.

After you have completed this program, produce an enhanced version that also outputs a graph showing the
acerage rainfall and the actual rainfall for each of the previous 12 months. Your program should also ask the
user whether they want to see the table or the graph. Include a loop that allows the user tos eee either format as
many times as they wish until they end the program.


 
*/

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

using namespace std;

// Gives the array strings for each of the 12 months. Sets a constant array.
const string month[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

// Precondition: Declares the arrays for the current and previous data.
void table( double current_data [], double previous_data [] );
void bar_graph ( double current_data [], double previous_data [] );
void file_output ( ofstream& outfile );

int main ()
{
    int answer;
    // Sets the arrays fr the outputs of x and y.
    double x[12] ={0,0,0,0,0,0,0,0};
    double y[12] = {0,0,0,0,0};
    char response='y';
    while (response == 'y' || response == 'Y')
    {
        cout << "Which format would you like the program to give?" << endl;
        cout << "(Press 1 for a table, 2 for bar graph)" << endl;
        cin >> answer;
        cout << endl;

        if ( answer == 1 )
        {
            bar_graph( x, y );
        }
        if ( answer == 2 )
        {
    // Calls the function and fills the arrays
            table ( x, y );
        }

    // This is only version 1.
        cout << "Would you like to use this program again? (Y for yes, anything else for no) ";
        cin >> response;
    }
}

// Functions
void table ( double current_data [], double previous_data [] )
{
    // Fills the array of Current Rainfall and Previous Rainfall at a limit of 12 cells.
    double Current_Ar [12];
    double Previous_Ar [12];

    // Calls to open the files of both the previous and rainfall.dat.
    ifstream instream_1;
    ifstream instream_2;

// Opens rainfall current.
instream_1.open ( "rainfall_current.dat" );

   // If the file fails to open it will close the program and output the statement.
if ( instream_1.fail( ) )
    {
        cout << "Please check if the file is saved properly. It could not open." << endl;
    }

// Opens previous rainfall.
instream_2.open ( "rainfall_previous.dat" );

   // If the file fails to open it will close the program and output the statement.
if ( instream_2.fail( ) )
    {
        cout << "Please check if the file is saved properly. It could not open." << endl;
    }

    // Loops the program until all of the elements in the array are filled.
    for( int i =0; i < 12; i++ )
        {
            // Will fill the array as long as there are < 12 numbers. After it will not fill them anymore.
            instream_1 >>  Current_Ar[i];
            instream_2 >> Previous_Ar[i];
        }
    // Sets up the bar graph/spacing for the bar graph.
    cout << setw(5) << "Month";
    cout << setw(20) << "Current RF";
    cout << setw(15) << "Previous RF";
    cout << setw(15) << "Average RF";
    cout << endl;
    cout << left;

    // Loops the output of _ until it is met. It sets up a boarder for the programs bar graph.
    for(int i=0; i<75; i++)
		{
		    cout<<"_";
		}
    // Additional spacing.
    cout<< endl;

    // Sets up the spacing and outputs of the averages/data for the program below the boarder.
    for ( int i = 0; i < 12; i++ )
	{
	    cout << setw(15) << month[i];
        cout << setw(15) << Current_Ar[i];
        cout << setw(15) << Previous_Ar[i];
        cout << setw(10) <<(Current_Ar[i] + Previous_Ar[i] ) / 2 << endl;
	}

	cout << endl;

}

// Functions
void bar_graph ( double current_data [], double previous_data [] )
{
    int Month;
    // Fills the array of Current Rainfall and Previous Rainfall at a limit of 12 cells.
    double Current_Ar [12];
    double Previous_Ar [12];

    // Calls to open the files of both the previous and rainfall.dat.
    ifstream instream_1;
    ifstream instream_2;

// Opens rainfall current.
instream_1.open ( "rainfall_current.dat" );

   // If the file fails to open it will close the program and output the statement.
if ( instream_1.fail( ) )
    {
        cout << "Please check if the file is saved properly. It could not open." << endl;
    }

// Opens previous rainfall.
instream_2.open ( "rainfall_previous.dat" );

   // If the file fails to open it will close the program and output the statement.
if ( instream_2.fail( ) )
    {
        cout << "Please check if the file is saved properly. It could not open." << endl;
    }

for( int i =0; i < 12; i++ )
        {
            // Will fill the array as long as there are < 12 numbers. After it will not fill them anymore.
            instream_1 >> Current_Ar[i];
            instream_2 >> Previous_Ar[i];
        }

// This will call the month they unput
cout << "Please input the month you wish to see the averages for: ";
cout << "( Months are inputted as 1-12. 1 = January, 2 = February, ETC.)" << endl;
cin >> Month;
cout << endl;

// Input calls
if ( Month == 1 )
    {
    cout << "*************************" << endl;
    cout << "::January:: " << endl;
    cout << "Current: " << Current_Ar[0] << endl;
    cout << "Previous: " << Previous_Ar[0] << endl;
    cout << "Change: " << Current_Ar[0] - Previous_Ar[0] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 2 )
    {
    cout << "*************************" << endl;
    cout << "::February:: " << endl;
    cout << "Current: " << Current_Ar[1] << endl;
    cout << "Previous: " << Previous_Ar[1] << endl;
    cout << "Change: " << Current_Ar[1] - Previous_Ar[1] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 3 )
    {
    cout << "*************************" << endl;
    cout << "::March:: " << endl;
    cout << "Current: " << Current_Ar[2] << endl;
    cout << "Previous: " << Previous_Ar[2] << endl;
    cout << "Change: " << Current_Ar[2] - Previous_Ar[2] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 4 )
    {
    cout << "*************************" << endl;
    cout << "::April:: " << endl;
    cout << "Current: " << Current_Ar[3] << endl;
    cout << "Previous: " << Previous_Ar[3] << endl;
    cout << "Change: " << Current_Ar[3] - Previous_Ar[3] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 5 )
    {
    cout << "*************************" << endl;
    cout << "::May:: " << endl;
    cout << "Current: " << Current_Ar[4] << endl;
    cout << "Previous: " << Previous_Ar[4] << endl;
    cout << "Change: " << Current_Ar[4] - Previous_Ar[4] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 6 )
    {
    cout << "*************************" << endl;
    cout << "::June:: " << endl;
    cout << "Current: " << Current_Ar[5] << endl;
    cout << "Previous: " << Previous_Ar[5] << endl;
    cout << "Change: " << Current_Ar[5] - Previous_Ar[5] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 7 )
    {
    cout << "*************************" << endl;
    cout << "::July:: " << endl;
    cout << "Current: " << Current_Ar[6] << endl;
    cout << "Previous: " << Previous_Ar[6] << endl;
    cout << "Change: " << Current_Ar[6] - Previous_Ar[6] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 8 )
    {
    cout << "*************************" << endl;
    cout << "::August:: " << endl;
    cout << "Current: " << Current_Ar[7] << endl;
    cout << "Previous: " << Previous_Ar[7] << endl;
    cout << "Change: " << Current_Ar[7] - Previous_Ar[7] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 9 )
    {
    cout << "*************************" << endl;
    cout << "::September:: " << endl;
    cout << "Current: " << Current_Ar[8] << endl;
    cout << "Previous: " << Previous_Ar[8] << endl;
    cout << "Change: " << Current_Ar[8] - Previous_Ar[8] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 10 )
    {
    cout << "*************************" << endl;
    cout << "::October:: " << endl;
    cout << "Current: " << Current_Ar[9] << endl;
    cout << "Previous: " << Previous_Ar[9] << endl;
    cout << "Change: " << Current_Ar[9] - Previous_Ar[9] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 11 )
    {
    cout << "*************************" << endl;
    cout << "::November:: " << endl;
    cout << "Current: " << Current_Ar[10] << endl;
    cout << "Previous: " << Previous_Ar[10] << endl;
    cout << "Change: " << Current_Ar[10] - Previous_Ar[10] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }

if ( Month == 12 )
    {
    cout << "*************************" << endl;
    cout << "::December:: " << endl;
    cout << "Current: " << Current_Ar[11] << endl;
    cout << "Previous: " << Previous_Ar[11] << endl;
    cout << "Change: " << Current_Ar[11] - Previous_Ar[11] << endl;
    cout << "*************************" << endl;
    cout << endl;
    }
}

My question is regarding if i should have the user input a file name they wish to output the information to within main or in another void function. Basically What is needed at this point is the user is asked to input a file name that they wish too save the information from the program to. IT is a user inputted file name, which is where I am confused

Recommended Answers

All 9 Replies

I strongly recommend that you first simplify the program thusly:

/*
File: Rainfall.cpp
Description: Write a program that reads in the average monthly rainfall for a city for each month
of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program
then prints out a nicely formnatted table showing the rainfall for each of the previous 12 months as well
as how much below or above average the rainfall was for each month. The average monthly rainfall is given
for months January-December in order. To obtain the actual rainfall for the previous 12 months, the program first
asks what the current month is and then asks for the rainfall figures for the previous 12 months.

After you have completed this program, produce an enhanced version that also outputs a graph showing the
acerage rainfall and the actual rainfall for each of the previous 12 months. Your program should also ask the
user whether they want to see the table or the graph. Include a loop that allows the user tos eee either format as
many times as they wish until they end the program.
*/

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

using namespace std;

// Gives the array strings for each of the 12 months. Sets a constant array.
const string month[12]= {"January", "February", "March",
                         "April", "May", "June",
                        "July", "August", "September",
                        "October", "November", "December"};


// Precondition: Declares the arrays for the current and previous data.
void read_data ( double current_data [], double previous_data [] );
void table( double current_data [], double previous_data []  );
void bar_graph ( double current_data [], double previous_data [] );
void file_output ( ofstream& outfile );

int main ()
{
    int answer;
    // Sets the arrays fr the outputs of x and y.
    double x[12] = {0,0,0,0,0,0,0,0};
    double y[12] = {0,0,0,0,0};

    read_data(x, y);

    char response='y';
    while (response == 'y' || response == 'Y')
    {
        cout << "Which format would you like the program to give?" << endl;
        cout << "(Press 1 for a table, 2 for bar graph)" << endl;
        cin >> answer;
        cout << endl;

        if ( answer == 1 )
        {
            bar_graph( x, y );
        }
        if ( answer == 2 )
        {
            // Calls the function and fills the arrays
            table ( x, y );
        }

        // This is only version 1.
        cout << "Would you like to use this program again? (Y for yes, anything else for no) ";
        cin >> response;
    }
}

// Functions
void read_data ( double current_data [], double previous_data [] )
{
    // Calls to open the files of both the previous and rainfall.dat.
    ifstream instream_1;
    ifstream instream_2;

// Opens rainfall current.
    instream_1.open ( "rainfall_current.dat" );

    // If the file fails to open it will close the program and output the statement.
    if ( instream_1.fail( ) )
    {
        cout << "Please check if the file is saved properly. It could not open." << endl;
    }

// Opens previous rainfall.
    instream_2.open ( "rainfall_previous.dat" );

    // If the file fails to open it will close the program and output the statement.
    if ( instream_2.fail( ) )
    {
        cout << "Please check if the file is saved properly. It could not open." << endl;
    }

    // Loops the program until all of the elements in the array are filled.
    for( int i =0; i < 12; i++ )
    {
        // Will fill the array as long as there are < 12 numbers. After it will not fill them anymore.
        instream_1 >> current_data[i];
        instream_2 >> previous_data[i];
    }
}


void table ( double current_data [], double previous_data [] )
{

    // Sets up the bar graph/spacing for the bar graph.
    cout << setw(5) << "Month";
    cout << setw(20) << "Current RF";
    cout << setw(15) << "Previous RF";
    cout << setw(15) << "Average RF";
    cout << endl;
    cout << left;

    // Loops the output of _ until it is met. It sets up a boarder for the programs bar graph.
    for(int i=0; i<75; i++)
    {
        cout<<"_";
    }
    // Additional spacing.
    cout<< endl;

    // Sets up the spacing and outputs of the averages/data for the program below the boarder.
    for ( int i = 0; i < 12; i++ )
    {
        cout << setw(15) << month[i];
        cout << setw(15) << current_data[i];
        cout << setw(15) << previous_data[i];
        cout << setw(10) <<(current_data[i] + previous_data[i] ) / 2 << endl;
    }

    cout << endl;

}


void bar_graph ( double current_data [], double previous_data [] )
{
    int Month;

    // This will call the month they unput
    cout << "Please input the month you wish to see the averages for: ";
    cout << "( Months are inputted as 1-12. 1 = January, 2 = February, ETC.)" << endl;
    cin >> Month;
    cout << endl;

    Month--;   // adjust the month number to match the array indices

    cout << "*************************" << endl;
    cout << "::" <<  month[Month]   << ":: " << endl;
    cout << "Current: " << current_data[Month] << endl;
    cout << "Previous: " << previous_data[Month] << endl;
    cout << "Change: " << current_data[Month] - previous_data[Month] << endl;
    cout << "*************************" << endl;
    cout << endl;
}

This should make the whole thing a lot easier to work with.

Now, as for how to get the file names, it would seem fairly straightforward to simply get the filenames in main(), and then pass it to the function(s) which reads in the data (the one I now call read_data(), in this case).

>> My question is regarding if i should have the user input a file name they wish to output the information to within main or in another void function

Either will work. My general preference in these things is to use a function. It really doesn't matter. I don't see any attempt to do either. I guess you are going to change it so that "rainfall_current.dat" and "rainfall_previous.dat" are no longer hard-coded, but are instead user-entered. Is that the task?

As for the question you didn't ask, but I'll commnet on anyway, you have twelve chunks of repeated code, one for each month. Surely you can change this to one chunk of code and substitute an array index. The code will look much cleaner.

What it is, is I need to output the information to a new file with a user defined name. Basically I give them the option to output the info to a file and then they name the new file they wish to name it. Here's what I have

cout << "Would you like to output this info to a file? ( A-Yes, B-No )" << endl;
        cin >> answer1;

        while ( answer1 == 'Y' || answer1 == 'y' )
        {
        string file_name; // strings are better to use than char
        ofstream ofile;

        cout << "What would you like to call your file? ";
        cin >> file_name;

        ofile.open(string(file_name+".txt").c_str()); // will make a file named after the user and save it as a text fil

        ofile.close();

        return 0;
        }

I keep running into a seg fault as well.

If you're promting the user to enter a filename to which you'll save the information, I'd recommend you ask for it in a separate function, since reading it from the main() function means sending the parameters when executing the program, either from a shortcut link or a shell call from another program.

Just use a function prompting the user (via printf or cout) to enter a filename.

Actually, passing the filenames by command line might be the best solution of all:

/*
File: Rainfall.cpp
Description: Write a program that reads in the average monthly rainfall for a city for each month
of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program
then prints out a nicely formnatted table showing the rainfall for each of the previous 12 months as well
as how much below or above average the rainfall was for each month. The average monthly rainfall is given
for months January-December in order. To obtain the actual rainfall for the previous 12 months, the program first
asks what the current month is and then asks for the rainfall figures for the previous 12 months.

After you have completed this program, produce an enhanced version that also outputs a graph showing the
acerage rainfall and the actual rainfall for each of the previous 12 months. Your program should also ask the
user whether they want to see the table or the graph. Include a loop that allows the user tos eee either format as
many times as they wish until they end the program.
*/

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

using namespace std;

// Gives the array strings for each of the 12 months. Sets a constant array.
const string Month[12]= {"January", "February", "March",
                         "April", "May", "June",
                         "July", "August", "September",
                         "October", "November", "December"
                        };


// Precondition: Declares the arrays for the current and previous data.
void read_data ( string filename, double data [] );
void table( ofstream& outputFile, double current_data [], double previous_data []  );
void bar_graph ( ofstream& outputFile, int month, double current_data [], double previous_data [] );

int main (int argc, char*argv[])
{
    // Sets the arrays fr the outputs of x and y.
    double current_data[12]  = {0,0,0,0,0,0,0,0};
    double previous_data[12] = {0,0,0,0,0,0,0,0};

    if (argc != 4)
    {
        cout << "usage: rainfall <current_datafile> <previous_datafile> <output_file>" << endl;
        exit(-1);
    }

    string currentFilename(argv[1]);
    string previousFilename(argv[2]);
    string outputFilename(argv[3]);

    read_data(currentFilename, current_data);
    read_data(previousFilename, previous_data);

    ofstream outputFile;

    outputFile.open(outputFilename.c_str());

    if (!outputFile.fail())
    {
        table(outputFile, current_data, previous_data);

        for (int month = 0; month < 12; month++)
        {
            bar_graph(outputFile, month, current_data, previous_data);
        }
    }
}

// Functions
void read_data ( string filename, double data [] )
{
    // Calls to open the files of both the previous and rainfall.dat.
    ifstream in;

    // Opens rainfall current.
    in.open ( filename.c_str() );

    // If the file fails to open it will close the program and output the statement.
    if ( in.fail( ) )
    {
        cout << "Please check if the file " << filename << " is saved properly. It could not open." << endl;
    }

    // Loops the program until all of the elements in the array are filled.
    for( int i = 0; i < 12; i++ )
    {
        // Will fill the array as long as there are < 12 numbers. After it will not read them anymore.
        in >> data[i];
    }
}


void table ( ofstream& outputFile, double current_data [], double previous_data [] )
{

    // Sets up the bar graph/spacing for the bar graph.
    outputFile << setw(5)  << "Month";
    outputFile << setw(20) << "Current RF";
    outputFile << setw(15) << "Previous RF";
    outputFile << setw(15) << "Average RF";
    outputFile << endl;
    outputFile << left;

    // Loops the output of _ until it is met. It sets up a boarder for the programs bar graph.
    for(int i=0; i<75; i++)
    {
        outputFile << "_";
    }
    // Additional spacing.
    outputFile << endl;

    // Sets up the spacing and outputs of the averages/data for the program below the boarder.
    for ( int i = 0; i < 12; i++ )
    {
        outputFile << setw(15) << Month[i];
        outputFile << setw(15) << current_data[i];
        outputFile << setw(15) << previous_data[i];
        outputFile << setw(10) <<(current_data[i] + previous_data[i] ) / 2 << endl;
    }

    outputFile << endl;
}


void bar_graph ( ofstream& outputFile, int month, double current_data [], double previous_data [] )
{
    outputFile << "*************************" << endl;
    outputFile << "::" <<  Month[month]   << ":: " << endl;
    outputFile << "Current: " << current_data[month] << endl;
    outputFile << "Previous: " << previous_data[month] << endl;
    outputFile << "Change: " << current_data[month] - previous_data[month] << endl;
    outputFile << "*************************" << endl;
    outputFile << endl;
}

Just curious, I had tried your code and I got "usage: rainfall <current_datafile> <previous_datafile> <output_file>

Process returned 255 (0xFF) execution time : 0.003 s
Press ENTER to continue."

What exactly is going on there?

void ( ofstream& outfile )
{
    cout << "Would you like to output this info to a file? ( A-Yes, B-No )" << endl;
    cin >> answer1;

    while ( answer1 == 'Y' || answer1 == 'y' )
    {
        char file_name; // strings are better to use than char
        ofstream ofile;

        cout << "What would you like to call your file? ";
        cin >> file_name;

        ofile.open(char(file_name+".txt").c_str()); // will make a file named after the user and save it as a text fil

        ofile.close();

    return 0;
    }
}

Does this look better? Also I get an error "Expected ')' before token '&'"

Just curious, I had tried your code and I got "usage: rainfall <current_datafile> <previous_datafile> <output_file>

Process returned 255 (0xFF) execution time : 0.003 s
Press ENTER to continue."

What exactly is going on there?

The version of the program I wrote requires command-line arguments. If you are using Code::Blocks (which I gather is the case), you can go to the Project menu and select Set Programs' Arguments to add the filenames: rainfall_current.dat rainfall_previous.dat rainfall_totals.dat When this is run, it should create a file named rainfall_totals.dat which will contain the generated tables.

Thank you for the help, finally got it.
Here's the final code. Thanks again.

/*
File: Rainfall.cpp (Version 3)
Description: Write a program that reads in the average monthly rainfall for a city for each month
of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program
then prints out a nicely formnatted table showing the rainfall for each of the previous 12 months as well
as how much below or above average the rainfall was for each month. The average monthly rainfall is given
for months January-December in order. To obtain the actual rainfall for the previous 12 months, the program first
asks what the current month is and then asks for the rainfall figures for the previous 12 months.

After you have completed this program, produce an enhanced version that also outputs a graph showing the
acerage rainfall and the actual rainfall for each of the previous 12 months. Your program should also ask the
user whether they want to see the table or the graph. Include a loop that allows the user tos eee either format as
many times as they wish until they end the program.

For a more elaborate version, also allow the user to request the table be output to a file of which the name is
entered by the user. This program does everything that version 1 does.

This version is like version 1 except that input is taken from a file and the output is sent to a file. There is no user to interact
with, so there should be no loops to loop the program.

*/

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

using namespace std;

// Gives the array strings for each of the 12 months. Sets a constant array.
const string month[12]= {"January", "February", "March",
                         "April", "May", "June",
                        "July", "August", "September",
                        "October", "November", "December"};


// Precondition: Declares the arrays for the current and previous data.
void read_data ( double current_data [], double previous_data [] );

void output( ofstream& ofile, double current_data [], double previous_data [] );

int main ()
{
    // Sets the max character.
    char file_name (20);
    ofstream ofile;
    // Sets the arrays fr the outputs of x and y.
    double x[12] = {0,0,0,0,0,0,0,0};
    double y[12] = {0,0,0,0,0};

    read_data(x, y);

    output (ofile, x, y );
}

// Functions
void read_data ( double current_data [], double previous_data [] )
{
    // Calls to open the files of both the previous and rainfall.dat.
    ifstream instream_1;
    ifstream instream_2;

// Opens rainfall current.
    instream_1.open ( "rainfall_current.dat" );

    // If the file fails to open it will close the program and output the statement.
    if ( instream_1.fail( ) )
    {
        cout << "Please check if the file is saved properly. It could not open." << endl;
    }

// Opens previous rainfall.
    instream_2.open ( "rainfall_previous.dat" );

    // If the file fails to open it will close the program and output the statement.
    if ( instream_2.fail( ) )
    {
        cout << "Please check if the file is saved properly. It could not open." << endl;
    }

    // Loops the program until all of the elements in the array are filled.
    for( int i =0; i < 12; i++ )
    {
        // Will fill the array as long as there are < 12 numbers. After it will not fill them anymore.
        instream_1 >> current_data[i];
        instream_2 >> previous_data[i];
    }
}

// Will input info to a file and writes the info to the file and saves it.
void output( ofstream& ofile, double current_data [], double previous_data [] )
{
    // Strings are better for file names
    string file_name;

    cout << "What would you like to call your file? ";
    cin >> file_name;
    // User inputted name
    ofile.open((file_name+".txt").c_str());

        int Month;

        Month--;   // adjust the month number to match the array indices

        for ( int i = 1; i < 12; i++) // Fills the data
        {
            Month = i;

            ofile << "*************************" << endl;
            ofile << "::" <<  month[Month]   << ":: " << endl;
            ofile << "Current: " << current_data[Month] << endl;
            ofile << "Previous: " << previous_data[Month] << endl;
            ofile << "Change: " << current_data[Month] - previous_data[Month] << endl;
            ofile << "*************************" << endl;
            ofile << endl;
        }

        // Sets up the bar graph/spacing for the bar graph.
        ofile << setw(5) << "Month";
        ofile << setw(20) << "Current RF";
        ofile << setw(15) << "Previous RF";
        ofile << setw(15) << "Average RF";
        ofile << endl;
        ofile << left;

    // Loops the output of _ until it is met. It sets up a boarder for the programs bar graph.
        for(int i=0; i<75; i++)
        {
        ofile << "_";
        }
    // Additional spacing.
    ofile << endl;

    // Sets up the spacing and outputs of the averages/data for the program below the boarder.
    for ( int i = 0; i < 12; i++ )
    {
        ofile << setw(15) << month[i];
        ofile << setw(15) << current_data[i];
        ofile << setw(15) << previous_data[i];
        ofile << setw(10) <<(current_data[i] + previous_data[i] ) / 2 << endl;
    }
    // Closes the file
    ofile.close();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.