[prog] C++

Kathryn Hogg kjh at flyballdogs.com
Tue Jan 20 09:21:40 EST 2004


wolf said:

> 	int seat [10] = {NULL};

This does not initialize all ten elements of the array and will only set
the first element to NULL.  Additionally, NULL has historically been a
#define
used to represent a pointer that points to nothing.  In C++ it is
generally implemented as

#define NULL 0
but there may be some compilers out there that inherit an old C definition:
#define NULL ((void *)0)

In C++, simply using 0 instead of NULL is preferred and you should never
use it (for readability) in an integer context.

In your case, you will need to explicitly initialize all the elements in
the array by putting them in the initializer list:

    int seat[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

of in a loop:
    for (int i = 0; i < 10; i++)
        seat[i] = 0;

> 	if (Choice = 1)

As somone else pointed out, you are doing an assignment here,  You
probably wanted to do a comparision.

If you are using gcc, there are compiler options that will warn you when
you make mistakes like this.  We compile all our code at work with:

g++ -pedantic -W -Wall -Wno-format-y2k -Wunused -Wnon-virtual-dtor \
 -Woverloaded-virtual -Wpointer-arith -Wcast-align -Wswitch -Wunitialized

In the cases where you want to do and assignment in a conditional
expression, just wrap it in parens:

   if ((f = somefunc()))  {
   ....
   }

Those flags would also have warned you about possibly using unitialized
variable seat, i think.

-- 
Kathryn
http://womensfootyusa.com


More information about the Programming mailing list