-
Help with Nested Switch
I'm not sure what I'm doing wrong, but I know that something isn't right.
managePassage is of type bool. A value, either true or false is returned by the called function. fileIn and fileOut are of type ifstream and ofstream, respectively.
The rest of the variable shouldn't come into play here, so I don't think it's necessary to explain what they are or to include any declarations or initializations. However, if you would find that information helpful then I'll be happy to provide it.
The problem I'm having is that the case always equates to 1, even when it should be 0. My functions seem to be working as they should be...everything was going fine until I got to this part.
Code:
int main()
{
managePassage = FindWordEW(fileOut, puzzle, currentWord);
while(fileIn)
{
switch(managePassage)
{
case 1:
break;
case 0:
managePassage = FindWordWE(fileOut, puzzle, currentWord);
switch(managePassage)
{
case 1:
break;
case 0:
fileOut << "not found" << endl;
}
}
fileIn.ignore(255, '\n');
fileIn.get(currentWord, MAXWORDLENGTH);
}
return 0;
}
-
Print out the value of managePassage with cout to help debugging. Are you sure its what you think it is.
It looks like you've already got the layout idea, but just in case, here's a simple example of an imbedded switch
Code:
#include <iostream>
int main(void)
{
bool b = true, c = false;
switch (b)
{
case true:
switch (c)
{
case true:
break;
case false:
break;
default:
break;
}
break;
case false:
break;
default:
break;
}
return(0);
}
-
I tried the cout << managePassage, and found something very strange.
When I do it before the while loop to display its initial value after calling the function it equals 0. However, when I call it inside of the loop, even right at the top, it equates to 1.
-
Break down the code to a smaller chunk and go from there. If you can't get it, post something that we can compile/run.
-
Maybe the problem isn't with this switch...
I've been playing with this thing for the past 9 hours so I'm going to go ahead and post a very simplified version of main() and 2 other necessary called functions just so you can get an idea of what's going on. I hope that it will compile...I'm picking pieces from all over the place. But if you can get this to work then the entire thing will work.
If you notice anything that just doesn't seem right, be sure to let me know.
Code:
#include <fstream.h>
#include <string.h>
//constants
const int MAXWORDLENGTH = 15;
const int PUZZLEROWS = 15;
const int PUZZLECOLS = 15;
//function prototypes
bool FindWordEW(ofstream&,
char[][PUZZLECOLS],
char[MAXWORDLENGTH + 1]);
void InitializePuzzle(char[][PUZZLECOLS] );
int main()
{
char puzzle[PUZZLEROWS][PUZZLECOLS];
char currentWord[MAXWORDLENGTH];
bool managePassage;
ifstream fileIn;
ofstream fileOut;
fileIn.open("words.dat");
fileOut.open("proj4.out");
fileIn.get(currentWord, MAXWORDLENGTH);
managePassage = FindWordEW(fileOut, puzzle, currentWord);
while(fileIn)
{
switch(managePassage)
{
case 0:
break;
default:
managePassage = FindWordWE(fileOut, puzzle, currentWord);
switch(managePassage)
{
case 1:
break;
case 0:
fileOut << "Not found." << endl;
}// end switch
}//end switch
fileIn.ignore(255, '\n');
fileIn.get(currentWord, MAXWORDLENGTH);
cout << currentWord << endl;
cout << managePassage;
}//end while
return 0;
}//end main;
void InitializePuzzle(char puzzle[][PUZZLECOLS])
{
ifstream fileIn;
fileIn.open("puz.dat");
int i = 0;
int x = 0;
//fills a 2-dimensional array with the contents of a file
for (x = 0; x <= PUZZLECOLS; x++)
{
for (i = 0; i <= PUZZLEROWS; i++)
{
fileIn.get(puzzle[x][i]);
}
}
return;
}
bool FindWordEW(ofstream& fileOut,
char puzzle[][PUZZLECOLS],
char word[MAXWORDLENGTH + 1])
{
int i;
int x;
int a = 0;
int b = 0;
int z = 0;
int y = 0;
int length;
int coord1, coord2;
InitializePuzzle(puzzle);
//find length of the word
length = strlen(word);
//string for comparison
char temp[MAXWORDLENGTH +1];
for (x = 0; x <= PUZZLEROWS; x++)
{
for (i = 0; i <= PUZZLECOLS; i++)
{
//check to see if the first character of word == puzzle[x][i]
if(word[z] == puzzle[x][i])
{
coord1 = x;
coord2 = i;
a = x; //store value of x for manipulation
b = i; //store value of y for manipulation
y = 0; //reset y
while (y < length)
{
//copies the first length number of letters from puzzle to temp
temp[y] = puzzle[a][b];
b--;
y++;
}
while (y < MAXWORDLENGTH +1)
{
//fill the garbage arrays of temp with garbage arrays of word so they are equal
temp[y] = word[y];
y++;
}
if (strcmp(temp,word)==0)
{
fileOut << "The word " << word << " is found starting at (" << coord1 << ",";
fileOut << coord2 << ") moving West." << endl << endl;
return true;
}
}
}
}
return false;
}
-
Crap...you can't compile that without the input and output files. I can post them if you like, though I'd hate for you to have to do that much work. A quick glance-over will be just fine:)
Thanks,
Kevin
-
It doesn't compile anyway. :D
You've typo'd the function name FindWordWE() (note WE intead of EW)
Also, there's a } missing in main.
-
So there is...seems I missed a brace and forgot that another function was necessary for compilation.
Am I returning true and false correctly to main? I'm really not sure if the way I'm doing it makes any sense.
-
>>Am I returning true and false correctly to main?
Well, you're saying:
>return (true);
and
>return (false);
which are obvously correct syntax. I can't tell anymore without stepping through the code.
Back to the switch's, why are you using them anyway? If you switching on a bool variable, there's only going to be 2 choices, and the code would probably benefit from using plain old "if" statements instead.
-
I'll give the if's a try again...didn't have any luck last time, but I'll give it a shot.
Thanks