Need help with passing structs into functions.

Hey all,

First time poster, hopefully someone could help me out.
Basically my program needs to read some data from a file into an array of structs (or struct of arrays, I'm not sure what the term is because we're just learning about structs). The data then needs to be printed out, creating a word search with the words that need to be found underneath.

here's my code:

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
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;

void readPuzzle(ifstream, int, int, char, char, int &, int &);
void printPuzzle(puzzleInfo);

const int MAX_ROWS = 20;
const int MAX_COLUMNS = 20;
const int MAX_WORD_COUNT = 25;
const int MAX_WORD_LENGTH = 32;

struct puzzleInfo
{
	int mRows;
	int mColumns;
	char mPuzzle [MAX_ROWS][MAX_COLUMNS];
	char mWords [MAX_WORD_COUNT][MAX_WORD_LENGTH]; 
};

int main()
{
	ifstream inputFile;
	puzzleInfo sRows, sColumns, sPuzzle, sWords;

	printPuzzle(sPuzzle);

	return EXIT_SUCCESS;
}

void readPuzzle(ifstream inputFile, puzzleInfo sRows, puzzleInfo sColumns,
				puzzleInfo sPuzzle, puzzleInfo sWords, int &row, int &column)
{
	int count = 0;
	int counter = 0;

	inputFile.open("puzzle.txt");
	if(inputFile.fail())
	{ 
		cout << "Error: failure to open file." << endl;
	}

	inputFile >> row;
	inputFile >> column;

	inputFile.peek();
	while(!inputFile.eof() && count < row)
	{
		inputFile.getline(sPuzzle.mPuzzle[count], MAX_COLUMNS);
		inputFile.get();
		count++;
	}

	while(!inputFile.eof() && counter < MAX_WORD_COUNT)
	{
		inputFile.getline(sPuzzle.mWords[counter], MAX_WORD_LENGTH);
		inputFile.get();
		counter++;
	}
}

void printPuzzle(puzzleInfo sPuzzle)
{
	cout << "*****************************\n";
	cout << "*        Find A Word        *\n";
	cout << "*****************************\n\n";

	cout << "Number of Rows: " << endl;
	cout << "Number of Columns: " << endl << endl;

	cout << "Puzzle\n";
	cout << "------\n\n";

	for (int i = 0; i < MAX_ROWS; i++)
	{
		cout << sPuzzle.mPuzzle[i];
	}

	cout << "Words\n";
	cout << "-----\n\n";

	for (int j = 0; j < MAX_WORD_COUNT; j++)
	{
		cout << sPuzzle.mWords[j];
	}
}


obviously there's a few holes in my work, but I'm stuck trying to pass the struct. If anyone see's anything else glaringly obvious that needs attending to, please don't hesitate to let me know!

Thanks in advance!!

Oh and yes this is a homework assignment and I've tried figuring this out for hours today but I'm completely stuck. Even if I could get a hint or be pointed in the right direction I'll be happy
Last edited on
I did not look through all your code but it is obvious that function printPuzzle should have the parameter declared as const puzzleInfo & instead of puzzleInfo

void printPuzzle( const puzzleInfo & );
Last edited on
Okay so i changed that, but when I go to build it I get three errors:

error C4430: missing type specifier - int assumed. Note C++ does not support default int,
error C2664: 'printPuzzle' cannot convert parameter 1 from 'puzzleInfo' to 'const int'
error C2143: syntax error : missing ',' before '&'

I'm so lost..
Oh thanks for the help vlad from moscow!
Topic archived. No new replies allowed.