I\'m having problem with my code. It runs, but doesn\'t display the calculations
ID: 669514 • Letter: I
Question
I'm having problem with my code. It runs, but doesn't display the calculations on the report.txt file. can any one help? The code has to read from a file called temperaturest.txt and do all the calculations using functions then displaying them in to another text file called report.txt
here is my code.
#include
#include
#include
#include
#include
using namespace std;
const int SIZE = 100;
void read_temperatures_from_file(string date, int tempHigh[], int tempLow[], int numOfDay);
void generate_report(int tempHigh[], int tempLow[], int history[], string range[], float aveHigh, float aveLow, int & maxHigh, int & maxLow, int numOfDay);
void print_report_to_file(int tempHigh[], int tempLow[], int history[], string range[], float aveHigh, float aveLow, int maxHigh, int maxLow, int numOfDay);
int main()
{
int tempHigh[SIZE];
int tempLow[SIZE];
int history[12] = { 0 };
float aveHigh = 0.0; // high temperature average
float aveLow = 0.0; //low temperature average
int maxHigh = 0; //max in high temperature
int maxLow = 0; //lowest in low temperature
int numOfDay = 0;
string range[] = { "<0|","<=9|","<=19|","<=29|","<=39|","<=49|","<=59|","<=69|","<=79|","<=89|","<=99|",">99|" };
// variable declarations here
read_temperatures_from_file("temperaturetest.txt", tempHigh, tempLow, numOfDay);
generate_report(tempHigh, tempLow, history, range, aveHigh, aveLow, maxHigh, maxLow, numOfDay);
print_report_to_file(tempHigh, tempLow, history, range, aveHigh, aveLow, maxHigh, maxLow, numOfDay);
system("Pause");
return 0;
}
void read_temperatures_from_file(string date, int tempHigh[], int tempLow[], int numOfDay)
{
ifstream infile; //reads file
infile.open("temperaturetest.txt");
cout << "Temperture on ";
cout << endl;
//if statement if file can't be found
if (!infile)
{
cout << "Error, file couldn't be found";
cout << endl;
exit(1);
}
getline(infile, date);
infile >> numOfDay;
infile.close();
cout << date << endl;
}
void generate_report(int tempHigh[], int tempLow[], int history[], string range[], float aveHigh, float aveLow, int & maxHigh, int & maxLow, int numOfDay)
{
int highSum = 0;
int lowSum = 0;
//forloop to calculate high temperature average
for (int i = 0; i < numOfDay; ++i)
{
highSum += tempHigh[i];
}
aveHigh = ((float)highSum) / numOfDay;
//forloop to calculate low temperature average
for (int i = 0; i < numOfDay; ++i)
{
lowSum += tempLow[i];
}
aveLow = ((float)lowSum) / numOfDay;
//forloop to find high temperature
for (int i = 0; i < numOfDay; ++i)
{
if (tempHigh[i] > maxHigh)
maxHigh = tempHigh[i];
}
//forloop to find lowest temperature
for (int i = 0; i < numOfDay; ++i)
{
if (tempLow[i] < maxLow)
maxLow = tempLow[i];
}
//histogram of high temperatures calculations
int histogramForm;
for (int i = 0; i < numOfDay; i++)
{
if (tempHigh[i] < 0)
{
history[0]++;
}
else if (tempHigh[i] > 99)
{
history[11]++;
}
else
{
histogramForm = (tempHigh[i] / 10 + 1);
history[histogramForm]++;
}
}
}
void print_report_to_file(int tempHigh[], int tempLow[], int history[], string range[], float aveHigh, float aveLow, int maxHigh, int maxLow, int numOfDay)
{
ofstream outfile;
outfile.open("report.txt");
//forloop to output file.
for (int i = 0; i < numOfDay; ++i)
{
outfile << tempHigh[i];
outfile << " ";
outfile << tempLow[i];
outfile << endl;
}
outfile << endl << endl;
//Will display high average to file
outfile << "Average high: ";
outfile << fixed << setprecision(1) << aveHigh << "F " << endl;
// will display low average to file
outfile << "Average Low: ";
outfile << fixed << setprecision(1) << aveLow << "F " << endl;
outfile << endl << endl;
//Will display highest temperature
outfile << "Highest temperature for the day: ";
outfile << maxHigh << "F " << endl;
//will display lowest temperature
outfile << "Lowest temperature of the day: ";
outfile << maxLow << "F " << endl << endl;
outfile << "Histogram of high temperatures: " << endl;
outfile << "Vertival axis: ";
outfile << " range of temperatures";
outfile << endl;
outfile << "Horizontal axis: ";
outfile << "number of temperatures in that range";
outfile << endl << endl;
//forloop to display histogram;
for (int i = 0; i < 12; i++)
{
if (i != 12)
{
outfile << range[i];
for (int j = 0; j < history[i]; j++)
outfile << "*";
}
else
{
outfile << range[i];
for (int j = 0; j < history[12]; j++)
outfile << "*";
}
outfile << endl << endl;
}
outfile.close();
}
Temperature text file is below
March 3, 2015
26
88 72
52 36
73 48
89 72
97 84
61 41
48 37
68 45
88 63
79 52
48 21
55 46
54 41
97 81
55 37
79 70
72 43
68 45
102 80
57 39
37 32
45 28
66 45
59 36
-1 -1
3 3
Explanation / Answer
Please specify the loction of the file correctly as shown in bold below. The code is correct and runs properly and gives the required output. The changes made are given in bold.
Program:
#include<stdio.h>
#include<iostream>
#include<fstream>
using namespace std;
const int SIZE = 100;
void read_temperatures_from_file(string date, int tempHigh[], int tempLow[], int numOfDay);
void generate_report(int tempHigh[], int tempLow[], int history[], string range[], float aveHigh, float aveLow, int & maxHigh, int & maxLow, int numOfDay);
void print_report_to_file(int tempHigh[], int tempLow[], int history[], string range[], float aveHigh, float aveLow, int maxHigh, int maxLow, int numOfDay);
//std::fstream infile("D:iles emperaturetest.txt", std::ios_base::in);
//std::fstream outfile("D:iles eport.txt", std::ios_base::in);
int main()
{
int tempHigh[SIZE];
int tempLow[SIZE];
int history[12] = { 0 };
float aveHigh = 0.0; // high temperature average
float aveLow = 0.0; //low temperature average
int maxHigh = 0; //max in high temperature
int maxLow = 0; //lowest in low temperature
int numOfDay = 0;
string range[] = { "<0|","<=9|","<=19|","<=29|","<=39|","<=49|","<=59|","<=69|","<=79|","<=89|","<=99|",">99|" };
// variable declarations here
read_temperatures_from_file("temperaturetest.txt", tempHigh, tempLow, numOfDay);
generate_report(tempHigh, tempLow, history, range, aveHigh, aveLow, maxHigh, maxLow, numOfDay);
print_report_to_file(tempHigh, tempLow, history, range, aveHigh, aveLow, maxHigh, maxLow, numOfDay);
system("Pause");
return 0;
}
void read_temperatures_from_file(string date, int tempHigh[], int tempLow[], int numOfDay)
{
ifstream infile; //reads file
infile.open("D:iles emperaturetest.txt", std::ios_base::out);
cout << "Temperture on ";
cout << endl;
//if statement if file can't be found
if (!infile)
{
cout << "Error, file couldn't be found";
cout << endl;
exit(1);
}
getline(infile, date);
infile >> numOfDay;
infile.close();
cout << date << endl;
}
void generate_report(int tempHigh[], int tempLow[], int history[], string range[], float aveHigh, float aveLow, int & maxHigh, int & maxLow, int numOfDay)
{
int highSum = 0;
int lowSum = 0;
//forloop to calculate high temperature average
for (int i = 0; i < numOfDay; ++i)
{
highSum += tempHigh[i];
}
aveHigh = ((float)highSum) / numOfDay;
//forloop to calculate low temperature average
for (int i = 0; i < numOfDay; ++i)
{
lowSum += tempLow[i];
}
aveLow = ((float)lowSum) / numOfDay;
//forloop to find high temperature
for (int i = 0; i < numOfDay; ++i)
{
if (tempHigh[i] > maxHigh)
maxHigh = tempHigh[i];
}
//forloop to find lowest temperature
for (int i = 0; i < numOfDay; ++i)
{
if (tempLow[i] < maxLow)
maxLow = tempLow[i];
}
//histogram of high temperatures calculations
int histogramForm;
for (int i = 0; i < numOfDay; i++)
{
if (tempHigh[i] < 0)
{
history[0]++;
}
else if (tempHigh[i] > 99)
{
history[11]++;
}
else
{
histogramForm = (tempHigh[i] / 10 + 1);
history[histogramForm]++;
}
}
}
void print_report_to_file(int tempHigh[], int tempLow[], int history[], string range[], float aveHigh, float aveLow, int maxHigh, int maxLow, int numOfDay)
{
ofstream outfile;
outfile.open("D:iles eport.txt", std::ios_base::out);
//forloop to output file.
for (int i = 0; i < numOfDay; ++i)
{
outfile << tempHigh[i];
outfile << " ";
outfile << tempLow[i];
outfile << endl;
}
outfile << endl << endl;
//Will display high average to file
outfile << "Average high: ";
//outfile << fixed << setprecision(1) << aveHigh << "F " << endl;
// will display low average to file
outfile << "Average Low: ";
// outfile << fixed << setprecision(1) << aveLow << "F " << endl;
outfile << endl << endl;
//Will display highest temperature
outfile << "Highest temperature for the day: ";
outfile << maxHigh << "F " << endl;
//will display lowest temperature
outfile << "Lowest temperature of the day: ";
outfile << maxLow << "F " << endl << endl;
outfile << "Histogram of high temperatures: " << endl;
outfile << "Vertival axis: ";
outfile << " range of temperatures";
outfile << endl;
outfile << "Horizontal axis: ";
outfile << "number of temperatures in that range";
outfile << endl << endl;
//forloop to display histogram;
for (int i = 0; i < 12; i++)
{
if (i != 12)
{
outfile << range[i];
for (int j = 0; j < history[i]; j++)
outfile << "*";
}
else
{
outfile << range[i];
for (int j = 0; j < history[12]; j++)
outfile << "*";
}
outfile << endl << endl;
}
outfile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.