Displaying Array Contents from File

For a part of my homework assignment (I'm in comp sci 1)I have to display the contents of a file. The contents is a 5x8 array of numbers. I thought I could place the contents of the file into an array, then display the contents of the array.

I checked a few places in my book but the ways they're describing how display an array are either confusing or I'm messing it up somehow.

Here's a snippet of my error messages:
|78|error: uninitialized const 'hospitalFloors' [-fpermissive]|
|81|error: invalid conversion from 'char' to 'const char (*)[8]' [-fpermissive]|

I have no clue how to appropriately interpret these errors. I wish they taught us how to quickly find solutions to errors. Is there a better way to do that than asking on these forums? I feel like there has to be some kind of manual that can help.

Here's my code. I guess for copy write reasons I have to say that the templete where I'm taking some of this code from was made by my professor and some is my own work. The definition of the prinTGrid is my own work. As well as the attempt to call the function.

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


using std::cout;
using std::endl;
using std::cin;
using std::ifstream;

const int FLOOR_ARRAY_SIZE = 5;
const int ROOM_ARRAY_SIZE = 8;

const char VACANT = 'V';
const char CHECKOUT = 'C';
const char OCCUPIED = 'O';
const char NURSE_STATION = 'W';
const char TRANSFER = 'T';
const char UTILITY = 'X';
const char INVALID = '@';

void printGrid(const char hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE]);

int main()
{

	const char hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE]; //Here's //where the first error is 
    // CODE HERE and FUNCTIONS GO AFTER MAIN
    // THIS SHOULD BE PRIMARILY FUNCTION CALLS
	printGrid(hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE]); //Here's the second error


    return 0;
}

// FUNCTIONS GO HERE
void printGrid(const char hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE])
{
	//Open Bed.txt file
	ifstream Bedsfile;
	Bedsfile.open("Beds.txt");

	//Place contents of Beds.txt into an array
	for (int floor = 0; floor < FLOOR_ARRAY_SIZE; floor++)
	{
		for (int room = 0; room < ROOM_ARRAY_SIZE; room++)
		{	//pg 423 displaying grid or pg 665 for displaying array
			cout << hospitalFloors[floor][room];
		}
	}

	//Display the array
	for (int floor = 0; floor < FLOOR_ARRAY_SIZE; floor++)
	{
		for (int room = 0; room < ROOM_ARRAY_SIZE; room++)
		{
			cout << Bedsfile;
		}
	}
	Bedsfile.close();
	cout << "Done. \n";
}
First error message: line 28 - you've described the array as const, but you haven't provided any values to initialize the array. Because it is const, you can't modify the array. The compiler is telling you there is no point in having a const array that is not initialized and that you can't change.

Second error message: line 31 - Do not supply the array dimensions when passing an array to a called function.
 
  printGrid(hospitalFloors);


line 44 says you're reading the beds.txt file, but that's not what your code is doing. Line 49 is displaying the contents of your empty array. BTW, you don't want to be reading in a print function. You want a different function to read the file into your non-const array.

line 58 - That's not displaying the contents of the array.


Thank you for your help AbstractinoAnon. I've taken out the <const> in line 28 (the line in main). The goal of this first part is to initialize the array with data from the Beds.txt file. I did more searching in my book and found out I can use a while loop to continually fill up the array. This technique seems to be less cumbersome, at least in my opinion. Also when passing the array as a function I took out the dimensions. A silly syntax error. However now I'm getting this strange error error: cannot convert 'char (*)[ROOM_ARRAY_SIZE]' to 'char (*)[8]' for argument '1' to 'void readGrid(char (*)[8])'|

Even after numerous searches I'm still lost as to why the compiler isn't converting the char to a char. To my knowledge that should be something it can do. I'm using the g++ C++11 ISO compiler.

Here's my edited code. Unfortunatly I don't acutally know if the new way of dumping the contents of the Beds.txt into the hospitalFloors array works since my code won't compile, but I have a feeling it should work since there's green on the left-hand side. I'm using code::blocks IDE.
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

#include <iostream>
#include <iomanip>
#include <fstream>


using std::cout;
using std::endl;
using std::cin;
using std::ifstream;

const int FLOOR_ARRAY_SIZE = 5;
const int ROOM_ARRAY_SIZE = 8;

// These functions will work in their current form. You must use them. You may add functions but
// don't remove them. Complete the pre and post condition comments.

//Function prototypes
/*
Pre and Post condition comments go here ... Prints the grid to the screen
*/
void printGrid(char hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE]);

/*
Pre and Post condition comments go here ... Reads in the initial grid from Beds.txt file
*/
void readGrid(char hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE]);

int main()
{
	int FLOOR_ARRAY_SIZE = 5;
	int ROOM_ARRAY_SIZE = 8;
	char hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE];
    // CODE HERE and FUNCTIONS GO AFTER MAIN
    // THIS SHOULD BE PRIMARILY FUNCTION CALLS
	readGrid(hospitalFloors); //Here's where the error pops up
	printGrid(hospitalFloors); //There's a similar error here


    return 0;
}

// FUNCTIONS GO HERE
 void printGrid(char hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE])
{
	//Open Bed.txt file
	ifstream Bedsfile;
	Bedsfile.open("Beds.txt");

	//Display the array
	for (int floor = 0; floor < FLOOR_ARRAY_SIZE; floor++)
	{
		for (int room = 0; room < ROOM_ARRAY_SIZE; room++)
		{
			cout << hospitalFloors [FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE];
		}
	}

	Bedsfile.close();
	cout << "Done. \n";
}


void readGrid(char hospitalFloors[FLOOR_ARRAY_SIZE][ROOM_ARRAY_SIZE])
{
	int index = 0;
	//Open the Beds.text file
	ifstream Bedsfile;
	Bedsfile.open("Beds.txt");

	for (int count = 0; count < FLOOR_ARRAY_SIZE; count++)
	{
		while (index < ROOM_ARRAY_SIZE && Bedsfile >> hospitalFloors[FLOOR_ARRAY_SIZE][index])
			index++;
	}

	//close the file
	Bedsfile.close();
}
line 31,32: Why are you declaring these again? You've already declared them at lines 12-13. Get rid of these lines.

Line 33: Array size declarations must be const. The arrays size declarations at lines 31-32 are not const.

After deleting lines 31-32, your program compiles fine.

line 47, 48, 59: Why are you opening beds.txt in your print function? There is no need for an ifstream in this function. Get rid of these lines.
Topic archived. No new replies allowed.