-
quick question
How would I be able to find certain words in a word search given in a specific file. The words are also stored in another file. Here is the code I have so far:
Code:
#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio>
#include<string>
#include<cstring>
using namespace std;
void FileCheck(ifstream&, char[]);
void ReadWMatrix(ifstream&, char wmatrix[25][25]);
void ReadWords(ifstream&, string words[25]);
void PrintOut(char wmatrix[25][25], string words[25]);
int main()
{
ifstream input;
char wmatrix[25][25];
string words[25];
ReadWMatrix(input, wmatrix);
ReadWords(input, words);
PrintOut(wmatrix, words);
return 0;
}
/*******************************************************/
void FileCheck(ifstream& In,char Name[])
{
do
{
cout << "File not found\n";
cout << "Enter File Name >";
cin >> Name;
In.open(Name);
}
while (In.fail());
}
/*******************************************************/
void ReadWMatrix(ifstream& input, char wmatrix[25][25])
{
input.open("wmatrix.dat");
while(!input.eof())
{
for(int i=0; i<19; i++)
for(int j=0; j<21; j++)
input >> wmatrix[i][j];
}
input.close();
}
/*******************************************************/
void ReadWords(ifstream& input, string words[25])
{
input.open("words.dat");
while(!input.eof())
{
for (int i=0; i<25; i++)
input >> words[i];
}
input.close();
}
/*******************************************************/
void PrintOut(char wmatrix[25][25], string words[25])
{
for(int i=0; i<19; i++)
{
for(int j=0; j<21; j++)
cout << wmatrix[i][j];
cout << endl;
}
for(int i=0; i<25; i++)
{
cout << words[i];
cout << endl;
}
}
-
> char wmatrix[25][25];
> string words[25];
Why not use an array of strings for both?
> while(!input.eof())
Read the FAQ on why using eof() in a control loop is bad.
> Here is the code I have so far:
Yeah, and what is your question?
-
"How would I be able to find certain words in a word search given in a specific file?"
-
Declare a container to hold all the words you want to search for. Read entire content of appropriate file into the container. Then read the file to search one word at a time. Search the container of words for the current word. If current word found in container of words do something. Go to the next word in the file to search.