I really need help on this program. I am supposed to put a list of words into a 2d array in a grid type fashion. I am a novice programmer and I am completely stuck. Any help would be awesome.
Here is the code that I have come up with so far.
CODE
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int ROWS = 10;
const int COLUMS = 10;
const int MAX_WORDS = 10;
void GetFileName(string filename, ifstream& fin, string wordList[]);
void ProcessTxtFile(string wordList[], int& location, int& location2, ifstream& fin, char puzzle[][COLUMS], int border[], int& direction);
void FillPuzzle(char puzzle[][COLUMS], char filler);
void DisplayPuzzle(char puzzle[][COLUMS], int border[]);
void PutWordInPuzzle(int& location, int& location2, int& direction, char puzzle[][COLUMS]);
void main()
{
ifstream fin;
string filename;
int direction = 0;
int location = 0;
int location2 = 0;
char filler = '.';
char puzzle[ROWS][COLUMS];
int border[10] = {0,1,2,3,4,5,6,7,8,9};
string wordList[MAX_WORDS];
GetFileName(filename, fin, wordList);
FillPuzzle(puzzle, filler);
ProcessTxtFile(wordList, location, location2, fin, puzzle, border, direction);
}
void GetFileName(string filename, ifstream& fin, string wordList[])
{
string word;
int count = 0;
int index = 0;
do
{
cout<< "Please enter the filename with the word list: ";
cin >> filename;
fin.open( filename.c_str() );
if (filename != "words.txt")
{
cerr<< "File does not exsist. Please try again." << endl;
fin.close();
fin.clear();
}
}
while ( filename != "words.txt" );
fin >> wordList[index];
while ( !fin.eof() )
{
index++;
fin >> wordList[index];
}
fin.close();
system("cls");
}
void FillPuzzle(char puzzle[][COLUMS], char filler)
{
/*srand(unsigned (time(NULL)));
(rand() %26 + 1);*/
for(int r=1; r<=ROWS; r++)
{
for(int c=1; c<=COLUMS; c++)
{
puzzle[r-1][c-1]= filler;
}
}
}
void DisplayPuzzle(char puzzle[][COLUMS], int border[])
{
cout << " WORD SEARCH " << endl;
cout << " 0 1 2 3 4 5 6 7 8 9 " << endl;
cout << " -------------------------------" << endl;
for (int r = 1; r <= ROWS; r++)
{
setiosflags(ios::right);
cout<< border[r-1] << " | ";
for(int c = 1; c <= COLUMS; c++)
{
cout<< puzzle[r-1][c-1] << " ";
}
cout<< "| " << border[r-1];
cout << endl;
}
cout << " -------------------------------" << endl;
cout << " 0 1 2 3 4 5 6 7 8 9 " << endl;
}
void ProcessTxtFile(string wordList[], int& location, int& location2, ifstream& fin, char puzzle[][COLUMS], int border[], int& direction)
{
for(int i=0; i < 5; i++)
{
DisplayPuzzle(puzzle, border);
cout<< "What row will " << wordList[i] << " begin on? ";
cin >> location;
cout<< "What column will " << wordList[i] << " begin on? ";
cin >> location2;
cout<< "What direction will it go in? " << endl
<< "1 2 3" << endl << " \ | / " << endl
<< "4-- --5" << endl << " / | \ " << endl
<< "6 7 8" << endl << "Choose direction 1 to 8: ";
cin >> direction;
system("cls");
PutWordInPuzzle(location, location2, direction, puzzle);
}
}
void PutWordInPuzzle(int& location, int& location2, int& direction, char puzzle[][COLUMS])
{
switch (direction)
{
case 1:
for(int r=location; r<ROWS; r--)
{
for(int c=location2; c<COLUMS; c--)
{
puzzle[r][c]= 'a';
}
}
break;
}
}
*edit: Please use code tags in the future, thanks!
This post has been edited by Martyr2: 1 Feb, 2008 - 01:30 PM