I/O question #2

I tried to get this answered on a separate thread but nothing was satisfactory or concrete. Listed below are two codes. The first is my code and it works perfectly giving me exactly the output I need. The second is slightly changed. On line 29 reader.is_open() is changed to reader.open(). I want to know what is the difference between these two lines because according to my last thread they should operate the exact same(if the retrieval folder is accessible). However in my case when I change my code they do not and I get an error with reader.open().
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
#include <iostream>
#include <fstream>


using namespace std;
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()
{
double calcius[7][24];
double fahrenheit;
double cuttoff;
double max=0;
double min=0;
double mean=0;
ifstream reader;
cout.precision(4);	//set precision to diplay 2 decimal digits only
reader.open("temperature.dat");
		if (reader.fail())
		{
		cout << "The program failed to read the data file.\n";
		return 0;
		}
	if (reader.is_open()) 
	{
		for(int i=0; i<7; i++)
		{
			for(int j=0; j<24; j++)
			{
			reader >> fahrenheit;
			calcius[i][j]=(fahrenheit-32)*5/9;	 //convert into calcius
			}
		}
	}
reader.close();
cout<<"\nEnter the value for which to find warmer temperatures (C): ";
cin>>cuttoff;
show_warmer(calcius, 7, cuttoff);
show_warmest(calcius, 7);
cout<<endl<<"The maximum, minimum, and mean temperatures for each day:\n"<<endl;
	for(int i=0; i<7; i++)
	{
	find_daily_mmm(calcius[i], max, min, mean);
	cout<<"Day "<<i+1<<": max "<<max<<" C, min "<<min<<"C, mean "<<mean<<" C."<<endl;
	}
	cout << "\n";
	return 0;
}




void show_warmer(double temperature[][24], int days, double cutoff)
{
cout<<"\nTimes at which temperatures warmer than "<<cutoff<<"C were found:\n"<<endl;
	for(int i=0; i<days; i++) 
	{
		for(int 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;
			}
		}
	}
}




void show_warmest(double temperature[][24], int days)
{
int maximum=0;
int maxday=0;
int maxhour=0;
cout<<endl<<"The warmest temperature was:\n"<<endl;
	for(int i=0; i<days; i++) 
	{
		for(int j=0; j<24; j++)
		{
			if(maximum<temperature[i][j])
			{
			maximum=temperature[i][j];
			maxday=i;
			maxhour=j;
			}
		}
	}
cout<<"At day "<<maxday+1<<", hour "<<maxhour+1<<", the temperature was "<<maximum<<" C."<<endl;
}


void find_daily_mmm(double day_temps[], double &max, double &min, double &mean){
double sum=0;
min=day_temps[0];
	for(int j=0; j<24; j++)
	{
		if(max<day_temps[j])
		{
		max=day_temps[j];
		}
		if(min>day_temps[j])
		{
		min=day_temps[j];
		}
		sum=sum+day_temps[j];
	}
mean=sum/24;
}

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
#include <iostream>
#include <fstream>


using namespace std;
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()
{
double calcius[7][24];
double fahrenheit;
double cuttoff;
double max=0;
double min=0;
double mean=0;
ifstream reader;
cout.precision(4);	//set precision to diplay 2 decimal digits only
reader.open("temperature.dat");
		if (reader.fail())
		{
		cout << "The program failed to read the data file.\n";
		return 0;
		}
	if (reader.open()) 
	{
		for(int i=0; i<7; i++)
		{
			for(int j=0; j<24; j++)
			{
			reader >> fahrenheit;
			calcius[i][j]=(fahrenheit-32)*5/9;	 //convert into calcius
			}
		}
	}
reader.close();
cout<<"\nEnter the value for which to find warmer temperatures (C): ";
cin>>cuttoff;
show_warmer(calcius, 7, cuttoff);
show_warmest(calcius, 7);
cout<<endl<<"The maximum, minimum, and mean temperatures for each day:\n"<<endl;
	for(int i=0; i<7; i++)
	{
	find_daily_mmm(calcius[i], max, min, mean);
	cout<<"Day "<<i+1<<": max "<<max<<" C, min "<<min<<"C, mean "<<mean<<" C."<<endl;
	}
	cout << "\n";
	return 0;
}




void show_warmer(double temperature[][24], int days, double cutoff)
{
cout<<"\nTimes at which temperatures warmer than "<<cutoff<<"C were found:\n"<<endl;
	for(int i=0; i<days; i++) 
	{
		for(int 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;
			}
		}
	}
}




void show_warmest(double temperature[][24], int days)
{
int maximum=0;
int maxday=0;
int maxhour=0;
cout<<endl<<"The warmest temperature was:\n"<<endl;
	for(int i=0; i<days; i++) 
	{
		for(int j=0; j<24; j++)
		{
			if(maximum<temperature[i][j])
			{
			maximum=temperature[i][j];
			maxday=i;
			maxhour=j;
			}
		}
	}
cout<<"At day "<<maxday+1<<", hour "<<maxhour+1<<", the temperature was "<<maximum<<" C."<<endl;
}


void find_daily_mmm(double day_temps[], double &max, double &min, double &mean){
double sum=0;
min=day_temps[0];
	for(int j=0; j<24; j++)
	{
		if(max<day_temps[j])
		{
		max=day_temps[j];
		}
		if(min>day_temps[j])
		{
		min=day_temps[j];
		}
		sum=sum+day_temps[j];
	}
mean=sum/24;
}
Last edited on
i might have just answered my own question is, is this below code correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
reader.open("temperature.dat");
		if (reader.fail())
		{
		cout << "The program failed to read the data file.\n";
		return 0;
		}
	{
		for(int i=0; i<7; i++)
		{
			for(int j=0; j<24; j++)
			{
			reader >> fahrenheit;
			calcius[i][j]=(fahrenheit-32)*5/9;	 //convert into calcius
			}
		}
	}
Last edited on
What errors where you getting?
it wouldn't run at all saying that the stream code was invalid something like std::
Topic archived. No new replies allowed.