0

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.

10
  • 1
    You can use an enum with values true and false Commented Jan 8, 2016 at 18:07
  • any reason you want to turn a C++ code into C? Commented Jan 8, 2016 at 18:09
  • 5
    Possible duplicate of Using boolean values in C Commented Jan 8, 2016 at 18:09
  • Use #define to create TRUE and FALSE. that way the usage in your code will be correct and you will not have to go through and edit the code beyond making sure that you are using the defined values (for example changing true to TRUE and false to FALSE) Commented Jan 8, 2016 at 18:09
  • Do you need all these lines to describe the problem? The question could be simplified to at most 20% of the current size. Commented Jan 8, 2016 at 18:12

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@Takari Do you know what an enum (enumeration) is? If not, try to look that up in your C textbook.
Sorry, but this is very bad advice. C has a native boolean type, so why not use it?
And an int behaves different. Try: _Bool b = 5; int i = 5; i == b; with your approach and in C99.
@Olaf OP specifically says that stdbool.h (and thus likely _Bool) is not available. Please read the question before downvoting.
@FUZxxl: I very well did. I also read the comments. And the question does not state OP is restricted to use non-standard C.
|
1

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.

5 Comments

I'm using Microsoft Visual C++ 2010 Express as I was recommended to use that by the lecturer. The code is in C++ as I found it online , however I was changing it into C and found out the boolean problem . At the moment I think I've managed to fix it . Thanks for the answers.
@Takari, no version of Microsoft Visual C++ conforms to C99 or C11, regardless of build mode or options. Microsoft has always been uninterested. I cannot recommend MSVC as your development platform if you really mean to build C code.
@Takari: So you got the task to write some code and try taking a shortcut copying something you found instead of writing your own. And now you notice it might not be that easy. Anyway - kick a** your lecturer, get a standard-compliant compiler (e.g. MinGW/gcc) and use at least C99 (better C11). There is only one valid C standard and that is C11 (although C99 is very similar and acceptable, too). MSVC is rubbish (at least the version you use).
@Olaf I actually like programming , but I never dived into it as I don't have any desire to be a programmer. I'm studying Business and Information Technology and unfortunately I have an assignment on C and I never programmed in C. ( I used Java in exams , HTML and Visual Basics ) I'm not trying to take a shortcut I simply don't have the knowledge to create it.
@Takari: Normally, if you get an assignment you're also augmented to finish it. If not, doubts arise about the overal quality of your school/university/whatever. A good indicator is already recommending MSVC for C programming. Not sure if there is a worse compiler for standard desktops which still is maintained. So my strong advice: get gcc and use stdbool.h. Sorry I cannot help with getting an IDE, I don't use Windows for Work.
1

C99 has the <stdbool.h> header.

8 Comments

OP says that stdbool.h is not available on his platform. Downvoting.
@FUZxxl, that the OP says so is probably a red herring. It indicates at least that his compiler does not conform to C99, which stands to reason because his code relies on C++-specific features. No C99 compiler would accept it. If he really does mean to convert to conforming C, then he should be able ultimately to rely on stdbool.h.
Upvoted; that is the only correct answer. According to a comment, OP just uses the non-compliant compiler MSVC10 because someone (with little knowldege about the C standard) recommended it. So OP can use a different toolchain. I agree with @JohnBollinger here).
@FUZxxl, yes, I did read the question. Since the OP said he tried stdbool.h and found that it didn't work for him, it is reasonable to conclude that he tested on a compiler that accepts his code if he does not use stdbool.h. For the code he presented, that can only be a C++ compiler, hence his test is flawed (in fact, he used MSVC++). Every conforming C99 and C11 compiler supports stdbool.h. "Use a conforming compiler and rely on the features of standard C" is hardly inappropriate advice to someone clearly engaged in an educational exercise.
@FUZxxl, before accusing me of insufficient attention to the question, perhaps you should review it yourself to evaluate which of your claims about it are actually there, and which are your own deductions. In any event, I suppose we'll have to agree to disagree about whether it is productive to urge a C neophyte to learn actual standard C, using a development platform that supports it. For his own part, the OP seems to have been receptive to that.
|
0

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

2 Comments

Sorry, but this is very bad advice. C has a native boolean type, so why not use it? Get a more recent (i.e. < 15 years old) compiler!
In his case I think that it is the best way to do it. Sorry if it is not your favourite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.