Dec 11, 2008 at 4:38am Dec 11, 2008 at 4:38am UTC
I am a student in a beginner's C++ class and my final project is to create a weather statistics tacking program.
Link to the assnmnt:
http://brahms.eecs.utoledo.edu/~jguernse/_pdf/project4.pdf
My code seems to be retarded and doesn't seem to work properly after 31 lines of output.
Here is the abomination:
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
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <fstream>
#include <time.h>
using namespace std;
// Author: nunya
// Project 4
int main (void ){
ifstream hourlyTemp;
ifstream averageTemp;
ifstream historicalTemp;
string filename;
double days[31][24];
//Attempting to open files
hourlyTemp.open("c:/temperature/File 1.txt" );
if (hourlyTemp.fail()) {
cout << "Couldn't open input file" << endl;
cin.ignore();
return EXIT_FAILURE;
}
averageTemp.open("c:/temperature/File 2.txt" );
if (averageTemp.fail()) {
cout << "Couldn't open input file" << endl;
cin.ignore();
return EXIT_FAILURE;
}
historicalTemp.open("c:/temperature/File 3.txt" );
if (historicalTemp.fail()) {
cout << "Couldn't open input file" << endl;
cin.ignore();
return EXIT_FAILURE;
}
for (int day = 0; day < 31; day++){
for (int hour = 0; hour < 24; hour++){
hourlyTemp >> days[day][hour];
cout << days[day][hour] << " " ;
}
cout << endl;
}
double lowtemp;
double lowtemps[31];
double hightemp;
double hightemps[31];
double averagetemp;
double averagetemps[31];
for (int day = 0; day<31; day++){
lowtemp = days [day][0];
for (int hour = 0;hour <24; hour++){
if (days[day][hour] < lowtemp){
lowtemp = days [day][hour];
}
}
lowtemps [day]=lowtemp;
cout << "Low temp for Day: " << lowtemp << endl;
}
for (int day = 0; day<31; day++){
hightemp = days[day][0];
for (int hour = 0; hour<24; hour++){
if (days[day][hour] > hightemp) {
hightemp= days [day][hour];
}
}
hightemps[day]=hightemp;
cout << " High temp for the Day: " << hightemp << endl;
}
for (int day = 0; day < 31; day++){
averagetemp = days[day][0];
for (int hour=0; hour < 24; hour++) {
averagetemp=averagetemp + days[day][hour];
}
averagetemp=averagetemp/24;
averagetemps[day]=averagetemp/24;
cout << "Average temp for the Day: " << averagetemp << endl;
}
cin.ignore();
cin.ignore();
return EXIT_SUCCESS;
}
Fun, eh?
Thank you in advance for the help. :)
P.S. Don't worry about the deadline, my professor gave us all a grace period. We are all stuggling...
Last edited on Dec 12, 2008 at 6:58pm Dec 12, 2008 at 6:58pm UTC
Dec 11, 2008 at 8:13am Dec 11, 2008 at 8:13am UTC
How exactly is it "not working properly"? Also...your code looks fine...what do the text files look like?
Dec 11, 2008 at 4:34pm Dec 11, 2008 at 4:34pm UTC
I have it so that it should be identical every 4 lines (to check and see if it is working correctly) and it isn't exactly correct!
File 1:
10 6 4 1 -2 -4 -3 -1 3 5 10 17 19 20 20 22 21 20 19 17 15 13 11
19 19 19 19 18 18 18 19 20 20 21 23 25 28 31 35 34 32 30 28 27 27 26
25 25 24 24 24 23 23 24 26 28 29 30 32 33 35 37 36 34 33 30 29 28 27
26 25 25 24 23 22 22 22 23 23 24 24 23 20 17 13 12 12 11 10 10 10 10
10 6 4 1 -2 -4 -3 -1 3 5 10 17 19 20 20 22 21 20 19 17 15 13 11
19 19 19 19 18 18 18 19 20 20 21 23 25 28 31 35 34 32 30 28 27 27 26
25 25 24 24 24 23 23 24 26 28 29 30 32 33 35 37 36 34 33 30 29 28 27
26 25 25 24 23 22 22 22 23 23 24 24 23 20 17 13 12 12 11 10 10 10 10
10 6 4 1 -2 -4 -3 -1 3 5 10 17 19 20 20 22 21 20 19 17 15 13 11
19 19 19 19 18 18 18 19 20 20 21 23 25 28 31 35 34 32 30 28 27 27 26
25 25 24 24 24 23 23 24 26 28 29 30 32 33 35 37 36 34 33 30 29 28 27
26 25 25 24 23 22 22 22 23 23 24 24 23 20 17 13 12 12 11 10 10 10 10
10 6 4 1 -2 -4 -3 -1 3 5 10 17 19 20 20 22 21 20 19 17 15 13 11
19 19 19 19 18 18 18 19 20 20 21 23 25 28 31 35 34 32 30 28 27 27 26
25 25 24 24 24 23 23 24 26 28 29 30 32 33 35 37 36 34 33 30 29 28 27
26 25 25 24 23 22 22 22 23 23 24 24 23 20 17 13 12 12 11 10 10 10 10
10 6 4 1 -2 -4 -3 -1 3 5 10 17 19 20 20 22 21 20 19 17 15 13 11
19 19 19 19 18 18 18 19 20 20 21 23 25 28 31 35 34 32 30 28 27 27 26
25 25 24 24 24 23 23 24 26 28 29 30 32 33 35 37 36 34 33 30 29 28 27
26 25 25 24 23 22 22 22 23 23 24 24 23 20 17 13 12 12 11 10 10 10 10
10 6 4 1 -2 -4 -3 -1 3 5 10 17 19 20 20 22 21 20 19 17 15 13 11
19 19 19 19 18 18 18 19 20 20 21 23 25 28 31 35 34 32 30 28 27 27 26
25 25 24 24 24 23 23 24 26 28 29 30 32 33 35 37 36 34 33 30 29 28 27
26 25 25 24 23 22 22 22 23 23 24 24 23 20 17 13 12 12 11 10 10 10 10
10 6 4 1 -2 -4 -3 -1 3 5 10 17 19 20 20 22 21 20 19 17 15 13 11
19 19 19 19 18 18 18 19 20 20 21 23 25 28 31 35 34 32 30 28 27 27 26
25 25 24 24 24 23 23 24 26 28 29 30 32 33 35 37 36 34 33 30 29 28 27
26 25 25 24 23 22 22 22 23 23 24 24 23 20 17 13 12 12 11 10 10 10 10
10 6 4 1 -2 -4 -3 -1 3 5 10 17 19 20 20 22 21 20 19 17 15 13 11
19 19 19 19 18 18 18 19 20 20 21 23 25 28 31 35 34 32 30 28 27 27 26
25 25 24 24 24 23 23 24 26 28 29 30 32 33 35 37 36 34 33 30 29 28 27
File 2:
22 -4 10.57
35 18 24.17
37 23 28.65
24 10 18.73
22 -4 10.57
35 18 24.17
37 23 28.65
24 10 18.73
22 -4 10.57
35 18 24.17
37 23 28.65
24 10 18.73
22 -4 10.57
35 18 24.17
37 23 28.65
24 10 18.73
22 -4 10.57
35 18 24.17
37 23 28.65
24 10 18.73
22 -4 10.57
35 18 24.17
37 23 28.65
24 10 18.73
22 -4 10.57
35 18 24.17
37 23 28.65
24 10 18.73
22 -4 10.57
35 18 24.17
37 23 28.65
File 3:
28
28
29
29
28
28
29
29
28
28
29
29
28
28
29
29
28
28
29
29
28
28
29
29
28
28
29
29
28
28
29
I'm not sure what to make of it, really.
Dec 12, 2008 at 6:57pm Dec 12, 2008 at 6:57pm UTC
Alright, I have finished the project, I have changed a LOT since I first posted and it actually works. Let me know what you think of it. I am quite proud of myself. :)
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 164 165 166 167 168 169 170 171 172 173 174 175 176
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <fstream>
#include <time.h>
using namespace std;
// Author: nunya
// Project 4
void main() {
float average;
int a, b, c, days, month, h;
ifstream input;
int X[31][24];
int avg[31] = {0};
int hist[31];
int histAvg[31];
int lowMeanHigh[31][3];
input.open ("File 1.txt" );
if (input.fail()) {
cout << "File 1 not found.\n" ;
cin.ignore();
exit(1);
}
do {
cout << "Enter month number :" ;
cin >> month;
if (month == 2)
days = 28;
else if (month == 4 || month == 6 || month == 9 || month == 11)
days = 30;
else
days = 31;
}
while (month < 0 && month > 13);
for (b = 0; b < days; b++) {
average=0;
for (a = 0; a < 24; a++) {
input >> h;
avg[b]+=h;
}
avg[b] = avg[b]/24;
}
input.close();
input.open ("File 3.txt" );
if (input.fail()) {
cout << "File 3 not found.\n" ;
cin.ignore();
exit(1);
}
for (a = 0; a < days; a++)
input >> histAvg[a];
input.close();
for (a = 0; a < days; a++)
hist[a] = avg[a] - histAvg[a];
cout << "Daily Average Temperature Histogram\n" << "Day +----+----+----+----+----+----+----|----+----+----+----+----+----+----+\n" ;
for (a = 0; a < days; a++) {
if (a < 9)
cout << a + 1 << " " ;
else
cout << a + 1 << " " ;
if (hist[a] < 0) {
h = 34 + hist[a];
do {
cout << " " ;
h--;
}
while (h > 0);
do {
cout << "*" ;
hist[a]++;
}
while (hist[a] < 0);
cout << "|" ;
}
else if (hist[a] > 0) {
h = 34;
do {
cout << " " ;
h--;
}
while (h > 0);
cout << "|" ;
do {
cout << "*" ;
hist[a]--;
}
while (hist[a] > 0);
}
else {
h = 34;
do {
cout << " " ;
h--;
}
while (h > 0);
cout << "|" ;
}
cout<<"\n" ;
}
input.open ("File 1.txt" );
if (input.fail()) {
cout << "File 1 not found.\n" ;
cin.ignore();
exit(1);
}
for (b = 0; b < days; b++) {
for (a = 0; a < 24; a++) {
input >> X[b][a];
}
}
input.close();
for (b = 0; b < days; b++) {
for (a = 0; a < 24; a++) {
for (c = 0; c <23; c++) {
if (X[b][c+1] < X[b][c]) {
h = X[b][c];
X[b][c] = X[b][c+1];
X[b][c+1] = h;
}
}
}
lowMeanHigh[b][0]=X[b][0];
lowMeanHigh[b][1]=X[b][11];
lowMeanHigh[b][2]=X[b][23];
}
input.open ("File 2.txt" );
if (input.fail()) {
cout << "File 2 not found.\n" ;
cin.ignore();
exit(1);
}
input.close();
cout << "\n\nDaily Lows, Medians, and Highs Chart\n" << "Day +----+----+----+----+----+----+----|----+----+----+----+----+----+----+\n" ;
for (b = 0; b < days; b++)
for (a = 0; a < 3; a++) {
lowMeanHigh[b][a] = lowMeanHigh[b][a] - histAvg[b] + 34;
}
for (b = 0; b < days; b++) {
if (b < 9)
cout << b + 1 << " " ;
else
cout << b + 1 << " " ;
for (a = 0; a < 70; a++) {
if (a == 34)
cout << "|" ;
else if (a == lowMeanHigh[b][0])
cout << "L" ;
else if (a == lowMeanHigh[b][1])
cout << "M" ;
else if (a == lowMeanHigh[b][2])
cout << "H" ;
else
cout << " " ;
}
cout << "\n" ;
}
cin.ignore();
cin.ignore();
//This program made me want to die. AHHHHHHHHHH!
}
Feel free to leave your impression of it. :) Thanks!
Last edited on Dec 12, 2008 at 6:57pm Dec 12, 2008 at 6:57pm UTC