Error - Need help fixing problems in program.


I am trying to write a program that will read some utility from a file, put it into a structure, and then write that structure to an output file. I am getting an error when trying to print and calc some uitility information in the second program. Also, the first program will not write anything to the utilities file, it only makes a blank txt file. can anyone help me figure out what I am doing wrong?

error in second program- "cpp: 73: error: request for member 'utility' in '*(((double)(((unsigned int)i)*8u))+ar)', which is of non-class type 'double'

Program 1:
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;
}


Program 2:

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;
					}
		}
}


Example 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  

You are trying to access a member of ar[i] called utility, but the type of ar[i] is double (since ar is an array of doubles).

slicedpan (114)

May 10, 2012 at 3:07pm



You are trying to access a member of ar[i] called utility, but the type of ar[i] is double (since ar is an array of doubles).




Report


I still don't understand what I am doing wrong. I can't call it this way? do I need to call it a different way to include structures? I am still lost.
Well a double is a built in type that represents a number. You are basically asking for the 'utility' of a number. That doesn't make any sense. However if you have a variable that has a type 'utilityInfo', you can legitimately ask about a member of that data type called 'utility' since it contains member data with that name.

1
2
3
4
5
6
7
8
9
10
utilityInfo myUtilInfo;
myUtilInfo.utility = "blah";    //this is fine

utilityInfo myUtils[10];
myUtils[0].utility = "foo";    //this is also fine

double aNumber;
aNumber.utility = "something";    //this is not possible, a double does not contain a 
                                                     //member called utility
Well a double is a built in type that represents a number. You are basically asking for the 'utility' of a number. That doesn't make any sense. However if you have a variable that has a type 'utilityInfo', you can legitimately ask about a member of that data type called 'utility' since it contains member data with that name.

1
2
3
4
5
6
7
8
9
10
utilityInfo myUtilInfo;
myUtilInfo.utility = "blah"; //this is fine

utilityInfo myUtils[10];
myUtils[0].utility = "foo"; //this is also fine

double aNumber;
aNumber.utility = "something"; //this is not possible, a double does not contain a
//member called utility


Ok I changed it but now I get the following errors.

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.
Topic archived. No new replies allowed.