Array of strings and doubles from a data file

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 -


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 /* 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>
using namespace 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);
const int 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!

EDIT: Grammar
Last edited on
Hello billycca3,

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.

Andy
Last edited on
Hello billycca3,

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.

Andy
Thanks Andy,

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!
Hello billycca3,

Why give up so easy. Even using what you started with it did not take long to rearrange what you had to make it work.

If you are willing to skip what you will learn here you will be making your future much more difficult.

Andy
Last edited on
I normally don't but after 8 hrs of trying and still having two projects to turn in, those took priority
Hello billycca,

OK I understand. Any time you want to work on this one let me know.

Andy
Topic archived. No new replies allowed.