/*
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