3

Possible Duplicate:
Using boolean values in C

I am newbie to C and want to write a program that will detect face from web cam, I got one on line,I am using opencv-2.4.3 on eclipse CDT, I searched on line for the solution but did not get the appropriate solution for my problem so posting it as new question.Here is the code:

 // Include header files
 #include "/home/OpenCV-2.4.3/include/opencv/cv.h"
 #include "/home/OpenCV-2.4.3/include/opencv/highgui.h"
 #include "stdafx.h"

 int main(){

//initialize to load the video stream, find the device
 CvCapture *capture = cvCaptureFromCAM( 0 );
if (!capture) return 1;

//create a window
cvNamedWindow("BLINK",1);

 while (true){
    //grab each frame sequentially
    IplImage* frame = cvQueryFrame( capture );
    if (!frame) break;

    //show the retrived frame in the window
    cvShowImage("BLINK", frame);

    //wait for 20 ms
    int c = cvWaitKey(20);

    //exit the loop if user press "Esc" key
    if((char)c == 27 )break;
}
 //destroy the opened window
cvDestroyWindow("BLINK");

//release memory
cvReleaseCapture(&capture);
return 0;
 }

And I am getting error as true’ undeclared (first use in this function), It is causing problem in while loop, I read it is not good practise to use while(true) but how should I go about. Can anybody hellp me out.

Community
  • 1
  • 1
shreya
  • 297
  • 3
  • 7
  • 15

3 Answers3

6

Replace it with e.g.

while(1)

or

for(;;)

or you can do (defining c before the loop):

while (c != 27)
{
    //grab each frame sequentially
    IplImage* frame = cvQueryFrame( capture );
    if (!frame)
        break;
    //show the retrieved frame in the window
    cvShowImage("BLINK", frame);
    //wait for 20 ms
    c = cvWaitKey(20);
    //exit the loop if user press "Esc" key
}

or without c at all, but this will start the loop with a 20ms wait:

while (cvWaitKey(20) != 27)
{
    //grab each frame sequentially
    IplImage* frame = cvQueryFrame( capture );
    if (!frame)
        break;
    //show the retrieved frame in the window
    cvShowImage("BLINK", frame);
}

And a third possibility:

for(;;)
{
    //grab each frame sequentially
    IplImage* frame = cvQueryFrame( capture );
    if (!frame)
        break;
    //show the retrieved frame in the window
    cvShowImage("BLINK", frame);
    if (cvWaitKey(20) == 27)
        break;
}

UPDATE: while wondering whether it would be more correct to define

#define true  1
#define false 0

or

#define true 1
#define false (!true)

or again

#define false 0
#define true  (!false)

because if I, say, did:

int a = 5;
if (a == true) { // This is false. a is 5 and not 1. So a is not true }
if (a == false){ // This too is false. So a is not false              }

I would come up with a really weird result, I found this link to a slightly weirder result.

I suspect that to solve this in a safe way would require some macro such as

#define IS_FALSE(a)  (0 == (a))
#define IS_TRUE(a)   (!IS_FALSE(a))
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • @Iserni- when i use c !=27, it gives me error as ‘c’ undeclared (first use in this function) and when I give without c. it gives me error in each and every syntax, as undefined refernce. – shreya Jan 17 '13 at 11:05
  • See answer: "(defining c before the loop)". You need to place an `int c;` declaration before the loop starts. However, you can use the c-less syntax: `while (cvWaitKey(20) != 27)` described below the first; of course, in that case you no longer have any `c` variable useable inside the loop. I have added a third possibility, anyway. – LSerni Jan 17 '13 at 11:10
3

true is not defined in many versions of c. If you want to use "boolean" see Using boolean values in C

Community
  • 1
  • 1
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • The definition of `true` would go in an include file (e.g. `curses.h` defines `TRUE`, uppercase). – LSerni Jan 17 '13 at 10:32
  • @UmNyobe- Thanks for immediate reply, I saw the link and tried usind stdbool but it is giving me same problem. – shreya Jan 17 '13 at 10:55
  • @Iserni - I tried including curses.h file but it gives me 26 error after including – shreya Jan 17 '13 at 11:04
1

The C compiler is pointing out that the variable true is not declared anywhere in your code nor in the header files that it includes. It's not part of the original C language specification. You may define it as a macro like so:

#define true  1

however it's simpler and clearer to use while(1). If you need an event loop, this is way it's usually done. If it's 'not good practice' that's news to me.

I keep forgetting about C99. You could also try adding

#include <stdbool.h>

if your version of C supports it.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Sue Mynott
  • 1,287
  • 1
  • 9
  • 14