Reputation: 29
So , unfortunately I encountered another problem with a program I'm trying to create. First of all I'm totally new to C Programming and I'm trying to create a Word Search .
I have this piece of code which is in C++ and I'm trying to turn it into C :
#include <iostream>
using namespace std;
int main()
{
char puzzle[5][5] = {
'A', 'J', 'I', 'P', 'N',
'Z', 'F', 'Q', 'S', 'N',
'O', 'W', 'N', 'C', 'E',
'G', 'L', 'S', 'X', 'W',
'N', 'G', 'F', 'H', 'V',
};
char word[5] = "SNOW"; // The word we're searching for
int i;
int len; // The length of the word
bool found; // Flag set to true if the word was found
int startCol, startRow;
int endCol, endRow;
int row, col;
found = false;
i = 0;
len = 4;
// Loop through each character in the puzzle
for(row = 0; row < 5; row ++) {
for(col = 0; col < 5; col ++) {
// Does the character match the ith character of the word
// we're looking for?
if(puzzle[row][col] == word[i]) {
if(i == 0) { // Is it the first character of the word?
startCol = col;
startRow = row;
} else if(i == len - 1) { // Is it the last character of the
// word?
endCol = col;
endRow = row;
found = true;
}
i ++;
} else
i = 0;
}
if(found) {
// We found the word
break;
}
}
if(found) {
cout << "The word " << word << " starts at (" << startCol << ", "
<< startRow << ") and ends at (" << endCol << ", " << endRow
<< ")" << endl;
}
return 0;
}
However , I've encountered a problem as I just noticed that C Programming doesn't support Booleans.
I'm using it so the user enters the word he is searching for ( for example: boy) , the user also enters the length ( 3 ) and then the user will enter the co-ordinates of the first and last letters of the word. When the user enters the following I'm planning to get the co-ordinates from the code above and than compare them with what the user entered. If they doesn't match the user guessed incorrectly , and if they match the user guessed it correctly.
I've also tried the stdbool.h
library , however it didn't work because the library wasn't found.
Is there any other way instead of stdbool.h
? I know you use true = 1 , false = 0 however I don't know exactly how to interpret it in the following code.
Thanks in advance.
Upvotes: 0
Views: 16481
Reputation: 180103
You say that
I've also tried the stdbool.h library , however it didn't work because the library wasn't found.
I'm inclined to suggest, then, that you find and use a conforming C99 or C2011 compiler. The former, at least, should not be too hard to put your hands on. Either will assuredly provide the header, and using it is probably your most convenient way forward.
Inasmuch as your code still contains some C++-isms (e.g. cout
, a using
statement, and C++-style #include
), I'm inclined to believe that you are compiling with a C++ compiler. That is one conceivable reason why the stdbool.h
header is not found. If you want to convert to C, then be sure to build your code with a C compiler.
Upvotes: 1
Reputation: 92966
Just use an int
instead. Otherwise, make your own boolean type:
enum { false, true };
typedef int bool;
If you want to store many booleans, consider
typedef char bool;
instead. Notice that neither make a “real” boolean type, as it can assume other values than just 0 and 1. Use the C convention that 0 indicates falsehood while every other value indicates truth.
Upvotes: 2
Reputation: 503
You can use a int
with 1
equal to true
or 0
equal to false
But I use this in my code at the top of the program, two constants to use the words TRUE
and FALSE
as if they were booleans:
#define FALSE 0
#define TRUE 1
Upvotes: 0