writing to a binary file

Hi everyone,
I am trying to write to a binary file from a 2D array. This is my first time doing this. Each line of the binary file format looks like:
floor(row) office(column) occupant type(enumerated type)

This is what I have so far, but I am getting this error: invalid operands of types 'int'and 'const char[2]' to binary operator <<'

Any help would be appreciated. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  //*****************************************************************************************************************
void writeFile(ofstream& outBuildingData, officeBuilding buildingArray, int flr, int col, int type)
{
	//variables	
	int floor, office; 
	
	for (floor = 0; floor < FLOOR_ROW; floor++) 
    	for (office = 0; office < OFFICE_COL; office++) 
    	{
    		outBuildingData << buildingArray[floor][office] = flr << " "
    						<< buildingArray[floor][office] = col << " "
    						<< buildingArray[floor][office] = type << endl;
		}
    		 
    outBuildingData.close(); 
	return;
}
//************************************************************* 


in main I have:
1
2
3
	//writing to the text file OUT_BUILDING.BIN
	ofstream outBuildingData;
	outBuildingData.open(BUILDING_FILE.c_str(), ios::binary);

1
2
3
4
5
6
		if (choice == 'D' || choice == 'd')		//if choice is 'D' exit the loop
		{
			cout << "Saving data to file BUILDING.BIN" << endl;
			writeFile(outBuildingData, buildingArray, flrNum, officeNum, enumType);	//function to save data to binary file
			return 1;
		}

You're not really showing enough content. For example what is an officeBuilding? What is the line that the compiler told you contains the problem?


I suggest you post the smallest possible complete program that illustrates the problem.
Sorry for that. officeBuilding is the variable name for my 2D array:
1
2
// typedef aliases
typedef int officeBuilding[FLOOR_ROW][OFFICE_COL];

1
2
3
4
int main()
{
	//variables	
	officeBuilding buildingArray;	//2D array 

The error appears for line 11 and 12
The variables flrNum, officeNum, and enumType are the numeric values for my array and the enumerated type value, as follows:
1
2
3
	int flrNum, 					//stores the floor number 
		enumType,					//stores the occupant type (numeric)
		officeNum,					//stores the office number 


I have a lot in my program, I was not able to give a complete program, but I included some functions that I think would help.
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//**************************************************************************
int main()
{
	//variables	
	officeBuilding buildingArray;	//2D array
	int officeCount = 0;			//total count for occupied offices in the binary file
	bool results;					//readFile return
	bool exitLoop = false;			//exits the main loop if true
	bool validatedRequest = true;	//if true, the original request is valid
	char choice;					//return value for user input from menu options
	int numLawyer = 0, 				//individual counters for Lawyer
		numParalegal = 0, 			//individual counters for paralegal
		numAssistant = 0; 			//individual counters for assisant
	int flrNum, 					//stores the floor number 
		enumType,					//stores the occupant type (numeric)
		officeNum,					//stores the office number
		total;						//stores the total amount of occupants in the 2D array
	char officeLetter;				//stores the office letter
	string occupantName;			//stores the string name of enumType
    
	//reading from input binary file for inBuildingData
	ifstream inBuildingData;
	inBuildingData.open(BUILDING_FILE.c_str(), ios::binary);  
	//writing to the text file OUT_BUILDING.BIN
	ofstream outBuildingData;
	outBuildingData.open(BUILDING_FILE.c_str(), ios::binary);
	
	display_header();						//display header
	initializeArray(buildingArray);			//initalize array to EMPTY
	results = readFile(inBuildingData, buildingArray, officeCount);	//read BUILDING.BIN file
	total = calculateCurrentStat(buildingArray, enumType);			//calculates and returns the individual types 
	
	while(choice != 'D' && choice != 'd')
	{
		
		choice = mainMenu();					//returns the verified input from main menu
		choice = menuValidation(choice);
		
		if (total == 0)							//if all offices are empty and choice is 1 or 2, display error and re-prompt menu
		{
			while (choice == '1' || choice == '2')
			{
				cout << endl;
				cout << "*** ERROR! ALL OFFICES ARE EMPTY. TRY AGAIN ***" << endl << endl;
				choice = mainMenu();			
				choice = menuValidation(choice);
			}
		}
		
		if (choice == 'D' || choice == 'd')		//if choice is 'D' exit the loop
		{
			cout << "Saving data to file BUILDING.BIN" << endl;
			writeFile(outBuildingData, buildingArray, flrNum, officeNum, enumType);	//function to save data to binary file
			return 1;
		}														
				
		cout << endl;
		choice = static_cast<int>(choice);		//change status of choice from char to int
		if(choice == '1' || choice == '2')										
		{			
			//validation functions
			do
			{
				displayOccupiedOffices(buildingArray);				//displays all occupied offices
				enumType = optionPrompts(flrNum, officeLetter);		//displays option 3 questions (floor number, column letter, and occupant type)
				officeNum = convertLetterToNumber(officeLetter);	//converts the office letter to a numeric value
				validatedRequest = validateOriginalRequest(flrNum, officeNum, enumType, choice, buildingArray);
			}while (!validatedRequest);	//loops until correct number and office letter are entered
			
			//display functions
			buildingArray[flrNum][officeNum] = enumType;			//adds the occupant type to the 2D array (enumerated type)
			occupantName = displayOccupantType(enumType);			//occupantName becomes the string value of the enumerated type name
			newOccupancyMessage(occupantName, flrNum, officeLetter, choice, buildingArray); 			//displays new occupant message
			total =calculateCurrentStat(buildingArray, enumType);	//re-calculates and displays all current status in the array			
		}
		else
		{
			//validation functions
			do
			{				
				displayEmptyOffices(buildingArray);					//displays all empty offices
				enumType = optionPrompts(flrNum, officeLetter);		//displays option 3 questions (floor number, column letter, and occupant type)
				officeNum = convertLetterToNumber(officeLetter);	//converts the office letter to a numeric value
				validatedRequest = validateOriginalRequest(flrNum, officeNum, enumType, choice, buildingArray);	
			}while (!validatedRequest);	//loops until correct number and office letter are entered
		
			//display functions
			buildingArray[flrNum][officeNum] = enumType;			//adds the occupant type to the 2D array (enumerated type)
			occupantName = displayOccupantType(enumType);			//occupantName becomes the string value of the enumerated type name
			newOccupancyMessage(occupantName, flrNum, officeLetter, choice, buildingArray); 			//displays new occupant message
			total =calculateCurrentStat(buildingArray, enumType);	//re-calculates and displays all current status in the array
		}
	}	
	cin.ignore();
	return 0;
}
//*****************************************************************************************************************
void initializeArray(officeBuilding buildingArray)
{
	//variables	
	int floor, office; 
	
	for (floor = 0; floor < FLOOR_ROW; floor++) 
    	for (office = 0; office < OFFICE_COL; office++) 
    		buildingArray[floor][office] = EMPTY;
      		 
	return;
}
//*****************************************************
//*****************************************************************************************************************
char mainMenu()
{
	//variables
	char choice;	
	
	cout << endl;
	cout << "**************************************************" << endl;
	cout << "\t\t\tMain Menu" << endl<< endl;
	cout << "\t1 - Change office from occupied to empty" << endl;
	cout << "\t2 - Modify office occupant type" << endl;
	cout << "\t3 - Change office from empty to occupied" << endl;
	cout << "\tD - Done making modifications" << endl << endl;
	cout << "***************************************************" << endl;
	
	cout << " Please enter menu choice (1, 2, 3, D): ";
	cin >> choice;
		
	return choice;
}
//****************************************
//************************************************************************************************
int menuValidation(char selection)
{
	while(selection) 
    {
        if( selection == 'D' || selection == 'd' ) 
			return selection; 

        else if( selection > '0' && selection < '4' ) 
			return selection; // if input is from 1-3

        else 
        {
        	cout << "ERROR! an invalid choice was entered. Enter new choice: " ; // bad input
			cin >> selection;
		}
    }
    return selection;
}
//******************************************************
//*****************************************************************************************************************
bool validateOriginalRequest(int flr, int office, int type, char choice, officeBuilding buildingArray)
{
	bool verify;
	
	if(choice == '1' || choice == '2')
	{
		if (buildingArray[flr][office] != EMPTY)
			verify = true;
		else
		{
			cout << endl;
			cout << "*** ERROR! Office is already empty. Please try again. ***" << endl;
			verify = false;
		}
	}
	else 
	{
		if (buildingArray[flr][office] == EMPTY)
			verify = true;
		else
		{
			cout << endl;
			cout << "*** ERROR! Office is already occupied. Please try again. ***" << endl;
			verify = false;
		}	
	}
	return verify;
}
//*************************************************



I hope this helps clear things up. Most of my program works , except for saving the data to the binary file. Thanks again for your help.
Last edited on
Topic archived. No new replies allowed.