- May 21, 2013
- 3,656
- 60
- 91
I'm in a beginning C++ class, and our current assignment was going well enough, until I hit my second function call.
The issue I'm having is in the function show_warmest. The cout to show where the loop is processing starts at [7][0], even though the variable i was initialized to 0 and the loop explicitly states that i starts at 0.
I would appreciate some guidance. I still have to write another function that parses the entire array, and I imagine I'll have the same kind of trouble there.
Code:
// File Name: program8.cpp
// Author:
// Student ID:
// Assignment Number: 8
// Description: This program reads temperatures from a data file and converts them to Celcius.
// Last Changed:
#include <iostream>
#include <iomanip>
#include <fstream>
// function declarations. Function format is required
void show_warmer(double temperature[][24], int days, double cutoff);
void show_warmest(double temperature[][24], int days);
void find_daily_mmm(double day_temps[], double &max, double &min, double &mean);
int main(void)
{
using namespace std;
// variable declaration
double target;
double celcius[7][24]; // required declaration
int i, j;
ifstream read_data;
// Always print the decimal and two decimal places.
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
// Opens temperature.dat, exits if file doesn't open
read_data.open("temperature.dat");
if (read_data.fail()) {
cout << endl << "File failed to open. Exiting..." << endl;
return -1;
}
// reads value from file into array, then converts value to celcius
for (i = 0; i <= 6; i++) {
for (j = 0; j <= 23; j++) {
read_data >> celcius[i][j];
celcius[i][j] = (celcius[i][j] - 32.0) * (5.0/9.0);
// cout << endl << "celcius[" << i << "][" << j << "] = " << celcius[i][j];
}
}
read_data.close();
cout << "Enter the value for which to find warmer temperatures (°C): ";
cin >> target;
cout << endl;
show_warmer(celcius, 7, target);
show_warmest(celcius, 7);
return 0;
}
void show_warmer(double temperature[][24], int days, double cutoff)
{
using namespace std;
int i, j;
cout << "Times at which temperatures warmer than " << cutoff << " were found:" << endl << endl;
for (i = 0; i < days; i++) {
for (j = 0; j < 24; j++) {
if (temperature[i][j] > cutoff) {
cout << "At day " << i+1 << ", hour " << j+1 << ", the temperature was "
<< temperature[i][j] << " °C." << endl;
}
}
}
cout << endl;
return;
}
void show_warmest(double temperature[][24], int days)
{
using namespace std;
double max = 0;
int i = 0, j = 0;
for (i = 0; i < days; i++); {
for (j= 0; j < 24; j++) {
cout << endl << "celcius[" << i << "][" << j << "] = " << temperature[i][j];
if (temperature[i][j] > max) {
max = temperature[i][j];
}
}
}
cout << "The warmest temperature was:" << endl << endl
<< "At day " << i+1 << ", hour " << j+1 << ", the temperature was "
<< temperature[i][j] << " °C." << endl << endl;
return;
}
The issue I'm having is in the function show_warmest. The cout to show where the loop is processing starts at [7][0], even though the variable i was initialized to 0 and the loop explicitly states that i starts at 0.
I would appreciate some guidance. I still have to write another function that parses the entire array, and I imagine I'll have the same kind of trouble there.
