I've been trying to solve this problem for 8+ hrs so far and can't even get the code to fill the arrays in right. I have to read from a text file about rain data and then compute certain things. The order of the .txt file is the name of the city at the very top then on the next line the month, year, and actual rainfall. This repeats 240 times so I have 240 different data points. Right now it looks like this -
/* This program computes the average rainfall overall, the average rainfall for
* each month, the four wettest, the four driest, and the 10 median months of
* rainfall.
*
* CS1410-030
* Last edited 10/3/2017
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
usingnamespace std;
// Function declaration
string overall_average (string input_data);
string monthly_averages (string input_data);
string wettest_months (string input_data);
string driest_months (string input_data);
string median_months (string input_data);
constint CAPACITY= 762;
int main() {
// Variables
string city = "";
string month[CAPACITY] = "";
string year[CAPACITY] = "";
string input[CAPACITY] = "";
// Input from file
ifstream in_file;
in_file.open("rainfall_data1.txt");
while(true) {
string input_data = "";
in_file >> input_data;
// End of file
if (in_file.fail()) {
break;
}
// Input Array
for (int j = 0; j<CAPACITY; j++) {
input[j] = input_data;
}
// City
city = input[0];
// These are just placeholders, still prints out the wrong data though
// Month Array
for (int k = 0; k < CAPACITY; k++) {
in_file >> month[k];
}
// Year array
for (int l = 0; l < CAPACITY; l++) {
year[l] = input_data;
}
}
// Output
ofstream output_file("rainfall_results1.txt");
output_file << "Assignment 5\n CS 1410\n NAME\n Rainfall data for " << city << endl;
// DEBUGGING
cout << input[0] << endl;
cout << "City: " << city << endl;
cout << "Month: " << month[1] << endl;
cout << "Year: " << year[1] << endl;
}
/* This function computes the overall average of rainfall from the input file.
*
* Parameters:
* input_data -- the data from the file.
*
* Return value:
* string -- the overall average of rainfall.
*/
string overall_average (string input_data) {
}
/* This function computes the monthly averages of rainfall from the input file.
*
* Parameters:
* input_data -- the data from the file.
*
* Return value:
* string -- the monthly averages of rainfall.
*/
string monthly_averages (string input_data) {
}
/* This function computes the overall wettest months from the input file.
*
* Parameters:
* input_data -- the data from the file.
*
* Return value:
* string -- the values of the wettest months.
*/
string wettest_months (string input_data) {
}
/* This function computes the overall driest months from the input file.
*
* Parameters:
* input_data -- the data from the file.
*
* Return value:
* string -- the values of the driest months.
*/
string driest_months (string input_data) {
}
/* This function computes the median months of rainfall from the input file.
*
* Parameters:
* input_data -- the data from the file.
*
* Return value:
* string -- the median months of rainfall.
*/
string median_months (string input_data) {
}
It currently prints out 12.51 as the city and input[0] but 12.51 is the very last value in the file and it should be printing Atlanta. Sorry, I know this is a long question but I don't know what else to do. Thanks for any and all help!
A note for the future. You have a green check mark on your subject line which lets people know that you have found an answer to your question. This should only be used after you have found an answer.
I will have to load up your program to what it is doing.
I could use a sample of the input file, so that I am working with the same data that you are.
I did some preliminary testing of your program an concluded that you need to rethink and rework the program, mostly the while loop and how you read the input file.
I do not know what you have learned so far, but a struct or class would be useful and an array or vector to hold the struct or class.
Defining your arrays with the size of "CAPACITY" is much more space than you will ever need.
Sadly I can't use structs for this one and after continuously attempting to fix it, I just decided to drop it. We've got one drop grade and it looks like it's this one. Thank you anyways!