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.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
Takari
  • 29
  • 2
  • 3
  • 8
  • 1
    You can use an enum with values `true` and `false` – loginn Jan 08 '16 at 18:07
  • any reason you want to turn a `C++` code into `C`? – bolov Jan 08 '16 at 18:09
  • 5
    Possible duplicate of [Using boolean values in C](http://stackoverflow.com/questions/1921539/using-boolean-values-in-c) – zvone Jan 08 '16 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) – sabbahillel Jan 08 '16 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. – Yu Hao Jan 08 '16 at 18:12
  • I never use formal boolean values. In C `0` is false and any other value is true. So I can write `int bananas=42; if(bananas) printf("Yes we have some bananas\n");` ... to misquote the song! – Weather Vane Jan 08 '16 at 18:20
  • C very well has native boolean types, for ca. 16 years now. Defining your onw types/constants is plain nonsense. – too honest for this site Jan 08 '16 at 18:29
  • @loginn, sabbahillel: Nonsense! C has a native boolean type and constants. – too honest for this site Jan 08 '16 at 18:29
  • @Olaf it is nonsense unless you can't use stdbool.h which OP clearly states – loginn Jan 08 '16 at 18:32
  • @loginn: The only valid C standard is C11. And since C99 which is now >15 years old, `_Bool` & co are mandatory features. According to another comment, OP can use a compliant compiler. Sticking with C90 if you can switch definitively **is** nonsense. It's not just for `_Bool`, but there are quite some other benefits which add to code-quality (e.g. required declaration of functions before usage, etc.). – too honest for this site Jan 08 '16 at 18:46

4 Answers4

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.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • @Takari Do you know what an `enum` (enumeration) is? If not, try to look that up in your C textbook. – fuz Jan 08 '16 at 18:11
  • 1
    Sorry, but this is very bad advice. C has a native boolean type, so why not use it? – too honest for this site Jan 08 '16 at 18:32
  • And an `int` behaves different. Try: `_Bool b = 5; int i = 5; i == b;` with your approach and in C99. – too honest for this site Jan 08 '16 at 18:52
  • @Olaf OP specifically says that `stdbool.h` (and thus likely `_Bool`) is not available. Please read the question before downvoting. – fuz Jan 08 '16 at 22:15
  • @FUZxxl: I very well did. I also read the comments. And the question does not state OP is restricted to use non-standard C. – too honest for this site Jan 08 '16 at 22:17
  • @Olaf From the rest of OP's comments, it's clear that OP uses MSVC which does not support C99. It does support standard C95, just not C99. OP makes it clear that his platform does not provide the `stdbool.h` header (and thus `_Bool`), so why do you keep insisting that no such restriction exists? – fuz Jan 08 '16 at 23:05
  • It is actually vita fort a beginner to use a modern (must I mention again that this still means supporting a 15 year old version of the standard) compiler and a more recent version of the standard. Otherwise we will continue answering such legacy problems over and over again. And yes, OP can change the the platform. He just uses that due to a [bad recommendation](http://stackoverflow.com/questions/34683326/boolean-in-c-programming/34683375?noredirect=1#comment57113792_34683693) – too honest for this site Jan 08 '16 at 23:36
1

C99 has the <stdbool.h> header.

MaddTheSane
  • 2,981
  • 24
  • 27
  • 1
    OP says that `stdbool.h` is not available on his platform. Downvoting. – fuz Jan 08 '16 at 18:12
  • 2
    @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`. – John Bollinger Jan 08 '16 at 18:33
  • 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). – too honest for this site Jan 08 '16 at 18:41
  • @JohnBollinger Have you actually read the question? OP's sample code is *C++ code he wants to translate to C* and the C compiler he uses (not the C++ compiler) has no `stdbool.h`. – fuz Jan 08 '16 at 22:17
  • 1
    @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. – John Bollinger Jan 08 '16 at 22:44
  • @JohnBollinger Okay, so you didn't read the question. **The code he presented is not the code he tried to include stdbool.h in.** The code OP presented is the code he tries to translate to C. And in the C file he wrote (which he didn't show to us), he could not include `stdbool.h`. And as a matter of fact, MSVC **does not support C99.** And asking OP to use a different platform **is not productive.** I'm sick of this thread. – fuz Jan 08 '16 at 23:03
  • 1
    @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. – John Bollinger Jan 08 '16 at 23:11
  • @JohnBollinger ANSI C is standard C, it's just a different standard than C11. – fuz Jan 09 '16 at 01:00
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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • 1
    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 Jan 08 '16 at 18:34
  • 2
    @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. – John Bollinger Jan 08 '16 at 18:38
  • @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). – too honest for this site Jan 08 '16 at 18:38
  • @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 Jan 08 '16 at 18:44
  • @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. – too honest for this site Jan 08 '16 at 18:51
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
Lopan
  • 483
  • 1
  • 7
  • 20