Need help With errors with utilityInfo and doubles

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
#include <iostream>
#include <iomanip>
#include <fstream>
 
using namespace std;
 
const int NUM_MON = 12;
 
struct utilityInfo
	{
	char utility[20];
	double monthlyExpenses[NUM_MON];
	};



void printUtilityStat( utilityInfo teamAr[], int numUtilities );
void calcStats( double ar[], int arSize, double &sumVal, double &avgVal, double &highVal, double &lowVal );

 
int main()
{

ifstream inFile2;
 
utilityInfo UtilitiesArray[20];
int numUtilities;
 
	inFile2.open( "binary_expenses", ios::binary );
		if( inFile2.fail() )
			  {
			  cout << "input file 2 did not open";
			  exit(-1);
			  }
	 
numUtilities = 0;
	 
	inFile2.read( (char *) &UtilitiesArray[numUtilities], sizeof(utilityInfo));
		 
		while( inFile2 )
			  {
			  numUtilities++;
			 
			  inFile2.read( (char *) &UtilitiesArray[numUtilities], sizeof(utilityInfo));
			  }
		 
	inFile2.close();
	 
		for( int sub = 0; sub < numUtilities; sub++ )
			  {
			  cout << UtilitiesArray[sub].utility << "\t" << UtilitiesArray[sub].monthlyExpenses[NUM_MON] << endl;
			   
			  }
 
inFile2.close();

cout << endl;
system("pause");
 
return 0;
}


void calcStats( double ar[], int arSize, double &sumVal, double &avgVal, double &highVal, double &lowVal )
{
	int i,j;
	for ( i = 0; i < 12; i++ )
		avgVal = sumVal / arSize;

	for ( i = 0; i < arSize;i++)
		{
			for(j = 0; j < 12; j++)
			sumVal += ar[i].utility[j];
		}
	
	if (ar[i] > highVal)
		highVal = ar[i];
		
	for ( i = 0; i < arSize;i++)
		{
			if (ar[i] < lowVal)
				lowVal = ar[i];
		}
}


void printUtilityStat( utilityInfo teamAr[], int numUtilities )
{
	double sumval, avgval, highval, lowval;
	int sub;
	int i;
      
	for(int j = 0; j < 5; j ++)
		{
			calcStats( teamAr[i].monthlyExpenses, numUtilities, sumval, avgval, highval, lowval );
				for ( sub = 0; sub < numUtilities; sub++)
					{
						cout << setprecision(2) << fixed << "sum over 12 month : $" << sumval;
						cout << " Average : $" << avgval << endl;
						cout << "highest cost : $" << highval ;
						cout << " lowest cost : $" << lowval << endl << endl;
					}
		}
}



cpp:33 error: no match for 'operator>'*(((utilityInfo)((((unsigned int)i)*120u))+ar)>highVal'

cpp:34 error: cannot convert 'utilityInfo' to 'double' in assignment

cpp:38 error: no match for 'operator>'*(((utilityInfo)((((unsigned int)i)*120u))+ar)>lowVal'

cpp:39 error: cannot convert 'utilityInfo' to 'double' in assignment


I think this is because I changed ar from double to utilityInfo. now I think I cannot associate this with the doubles highVal and lowVal, but I don't know how or what to change these values to to have this inequality work. After this I should be ok but this part is really confusing me. If there is a better way to do this please let me know. THANKS IN ADVANCE!!!

p.s. I included the first file and output for reference.

Program 1 (that writes the file for this program):

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

using namespace std;

const int NUM_MON = 12;

struct utilityInfo
	{
	char utility[20];
	double monthlyExpenses[NUM_MON];
	};

int main()
{

ifstream inFile;
ofstream outFile;

inFile.open("expenses2.txt");

	if( inFile.fail() )
		  {
		  cout << "input file did not open";
		  exit(-1);
		  }

outFile.open( "binary_utilities", ios::binary );

	if( outFile.fail() )
		  {
		  cout << "output file did not open";
		  exit(-1);
		  }


char s[20];
utilityInfo read;
int i;

inFile.getline( s, 20 );

while( inFile )
	{
		strcpy(read.utility,s);
		
		for (i = 0; i < NUM_MON; i++)
			{
				 inFile.getline( s, 20 );
				 read.monthlyExpenses = atof(s);
			 }
		
	outFile.write( (char *) &read, sizeof(utilityInfo));
		
		  inFile.getline( s, 20 );
	}


inFile.close();
outFile.close();


cout << endl;
system("pause");

return 0;
}


Output:

*** gas *** 
sum over 12 months: $1400.23        average:     $ 116.69 
highest cost:       $ 207.14        lowest cost: $  52.55 

*** electricity *** 
sum over 12 months: $1149.10        average:     $  95.76 
highest cost:       $ 188.32        lowest cost: $  50.90 

*** water *** 
sum over 12 months: $ 629.84        average:     $  52.49 
highest cost:       $  78.33        lowest cost: $  40.56 

*** cable *** 
sum over 12 months: $1310.95        average:     $ 109.25 
highest cost:       $ 152.64        lowest cost: $  46.93 

*** phone *** 
sum over 12 months: $ 957.65        average:     $  79.80 
highest cost:       $  83.57        lowest cost: $  79.15
Last edited on
closed account (4z0M4iN6)
I suggest, you should also post the buggy code - this file which containes such expessions: *(((utilityInfo)((((unsigned int)i)*120u))+ar)>highVal
Last edited on
Topic archived. No new replies allowed.