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

using namespace std;

const int MAX_MONTH = 13;
const int MAX_STRING_MONTH = 20;
const int MAX_ARRAY_TEMP = 13;
const int MAX_STRING_TEMP = 4;

void instructions ();

// Explains program to user

bool man_or_file(bool& manual, char& m_or_f);

// Determines if user wants to input manually or from a file.

void get_average_from_file(ifstream& avg_stream, char average[][MAX_STRING_TEMP\
],int& num_temps,int max_size);

// fills array with data from the average file

void get_2007_rainfall_frm_file(ifstream& stream_07, char rainfall[][MAX_STRING\
_TEMP],int& num_temps, int max_size);

// fills a second array with data from 2007 temps file

void display_month(char names_of_month[][MAX_STRING_MONTH],int max_month);

//Uses a for loop to display month names.


int main(){

  int num_temps,num_temps_07;
  ifstream avg_stream,stream_07;
  char average[MAX_ARRAY_TEMP][MAX_STRING_TEMP];
  char rainfall[MAX_ARRAY_TEMP][MAX_STRING_TEMP];
  char names_of_month[0]= "January";
avg_stream.open("average_rainfall.txt");{

  //Make sure file is opened correctly.

    if(avg_stream.fail())
      {
        cout << "Input file opening failed.\n";
        exit(1);
      }
  }
  stream_07.open("2007_rainfall.txt");{

    //Make sure file is opened correctly.

      if(stream_07.fail())
        {
          cout << "Input file opening failed.\n";
          exit(1);
        }
  }

  instructions();
  get_average_from_file(avg_stream,average,num_temps,MAX_STRING_TEMP);
  for (int i = 0;i < num_temps; i++)
    {
      cout <<avg_stream[i]<<endl;
    }
  get_2007_rainfall_frm_file(2007_stream,rainfall,num_temps_07,MAX_STRING_TEMP)\
;

  for (int x = 0; x < num_temps; x++)
    {
      cout <<rainfall[x]<<endl;
    }

  display_months(names_of_month,MAX_MONTH);

  return 0;


}


//Function definitons

void get_average_from_file(ifstream& avg_stream, char average[][MAX_TEMP_LENG\
TH],int& num_temps,int max_size){

  using namepspace std;

  int num_temps = 0;

  while((avg_stream.eof() == false) && (num_temps < MAX_TEMP_ARRAY))

    {

      avg_stream.getline(average[num_temps],  MAX_TEMP_ARRAY);

      num_temps++;
    }

}


  void get_2007_rainfall_frm_file(ifstream& 2007_stream, char rainfall[][MAX_TE\
MP_LENGTH],int& num_temps, int max_size){

    using namepspace std;

    int num_temps = 0;

    while((2007_stream.eof() == false) && (num_temps < MAX_TEMP_ARRAY))

      {

        2007_stream.getline(rainfall[num_temps],  MAX_TEMP_ARRAY);

        num_temps++;
      }

  }


  void display_month(char names_of_month[],int max_month){

    for (int i = 0; i < MAX_MONTH; i++)

      cout<< names_of_month[i]<<endl;


  }

This is my problem "Write a C++ program that reads in the historical average monthly rainfall for , NY for each month of the year and then reads in the actual monthly rainfall for each month in 2007. Finally, the program should print out a nicely formatted table showing the average rainfall for each month, the total rainfall for each month of 2007, and how much above or below average the rainfall was for each month. The output should correctly label the months.

Your program should give the user the option of entering all data from the command line or reading the data from directly from two files. Data files for this problem can be downloaded using the following commands:

wget http://.edu/class/cs141/fall08/labs/average_rainfall.txt
wget http://.edu/class/cs141/fall08/labs/2007_rainfall.txt

There are a various ways of dealing with the month names. One straightforward way is to code the months as integers and then do a conversion before doing output. A large switch statement is acceptable in an output function. The month-by-month input for manual data entry can be done any way you like, so long as it is relatively easy and pleasant for the user.

Be sure to make good use of functions in your program. At a minimum, you should include separate functions for each input method and for generating the results. You may also decide to create a function for outputting the month names.

A sample run of the program, for which the user chooses to enter the data manually, is shown below."

polaris ~> ./rainfall
This program analyzes monthly rainfall data
for the previous year against historical averages.

Would you prefer to enter the data manually or read it from files (m/f)? m

Please enter the historical monthly average rainfall data below.
January: 2.38
February: 2.02
March: 2.4
April: 2.98
May: 3.07
June: 3.28
July: 3.6
August: 4.05
September: 4.27
October: 3.33
November: 3.4
December: 2.7

Please enter the actual monthly rainfall amounts for the past year below.
January: 1.86
February: 0.13
March: 1.85
April: 3.15
May: 2.42
June: 2.2
July: 5.19
August: 0.38
September: 2.9
October: 4.68
November: 3
December: 2.09

A comparison of actual monthly rainfall amounts against
the historical monthly averages is shown below.

Month Average Actual Diff
----- ------- ------ ----
January 2.38 1.86 -0.52
February 2.02 0.13 -1.89
March 2.43 1.85 -0.58
April 2.98 3.15 +0.17
May 3.07 2.42 -0.65
June 3.28 2.20 -1.08
July 3.60 5.19 +1.59
August 4.05 0.38 -3.67
September 4.27 2.90 -1.37
October 3.33 4.68 +1.35
November 3.40 3.00 -0.40
December 2.70 2.09 -0.61

I'm trying to solve this problem with the parts of the problem I know how to handle first. I have started to tackle inputting the data from the two files given into two arrays that will store those. I need to also store each month name to be able to print those out with my table at the end. I am having trouble coming up with the function to store the month names. How would I initialize each month name? My other question is how should I handle when the user manually enters the data. Should I have a bool function that returns true or false and kick into either my file input functions or manual functions? let me know if anyone has any input! thank you!I know its a long post sorry:(

Best way to handle the problem is to start commenting parts out until it compiles. Start by commenting out all the function declarations and the functions and everything but this:

int main ()
{
     return 0;
}

When that compiles, uncomment the #include statements and const declarations:

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

using namespace std;

const int MAX_MONTH = 13;
const int MAX_STRING_MONTH = 20;
const int MAX_ARRAY_TEMP = 13;
const int MAX_STRING_TEMP = 4;

int main ()
{
     return 0;
}

If that all compiles, uncomment the function declarations:

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

using namespace std;

const int MAX_MONTH = 13;
const int MAX_STRING_MONTH = 20;
const int MAX_ARRAY_TEMP = 13;
const int MAX_STRING_TEMP = 4;

void instructions ();

// Explains program to user

bool man_or_file(bool& manual, char& m_or_f);

// Determines if user wants to input manually or from a file.

void get_average_from_file(ifstream& avg_stream, char average[][MAX_STRING_TEMP\
],int& num_temps,int max_size);

// fills array with data from the average file

void get_2007_rainfall_frm_file(ifstream& stream_07, char rainfall[][MAX_STRING\
_TEMP],int& num_temps, int max_size);

// fills a second array with data from 2007 temps file

void display_month(char names_of_month[][MAX_STRING_MONTH],int max_month);

//Uses a for loop to display month names.
int main ()
{
     return 0;
}

Once that all compiles, start uncommenting one function at a time till all the functions compile. Then and only then, start uncommenting your main function one line at a time from top to bottom till you hit your first error. You have some stray characters in there that are easy fixes, but need to be fixed before you can really start debugging, Fix the top error, ignore the other errors, recompile each time. You have syntax errors you need to fix before starting on the questions that you have in this post. Get a program that compiles before trying to tackle any of those issues that you list. Post updated code if you can't solve something. Also, the internet links you list don't lead anywhere.

This article has been dead for over six months. Start a new discussion instead.