Error: Incompatible type in Assignment

I seriously need some help! I get error "cpp.51 error: Incompatible type in assignment of 'double' to 'double[12]'." I am trying to get this program to write some information into a structure, and then write that structure to an output file. Also, I cannot get this to work because I cannot open toe file from program 2. Can anyone help???

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
#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 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;
}
Line 12: double monthlyExpenses[NUM_MON]; //Its an array of doubles
Line 51: read.monthlyExpenses = atof(s); //you are trying to assign a double to array
either use
1
2
double monthlyExpenses; //a double variable
read.monthlyExpenses = atof(s); //now it will be correct 

or
1
2
double monthlyExpenses[NUM_MON]; //a double variable array
read.monthlyExpenses[some_int] = atof(s); //now it will be correct 
Since error in first program
It never compile -> no file made -> no data written
Therefore, second program has no file to get data
Line 51 of program 1 must (certainly) be: read.monthlyExpenses[i] = atof(s); // Note [] for array access

For program 2: "binary_expenses" is likely not where the program expect it. Use an absolute path instead (e.g. "c:\\tmp\\binary_expenses" // Note \\ for windows )
THANKS GUYS!!!
the error
cpp.51 error: Incompatible type in assignment of 'double' to 'double[12]
says in the program line 51 you are trying to assign a value of data type to a variable of different data type.
1
2
3
4
5
6
double doublesArray [];
double doubleVariable;
doublesArray = doubleVariable; //  Error
doublesArray[i] = doubleVariable; //  OK
doubleVariable = doublesArray //  Error
doubleVariable = doublesArray[i] //  OK 

I hope it is clear
Yes, very clear. There are no more errors, now it's not displaying anything. Also the file created has nothing in it. The output just recognizes the system pause, but nothing else. Any suggestions?

I was thinking void printUtilityStat (utilityInfo teamAr[], int numUtilities)
Is this close?

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