Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > dice generator problems

Reply
Thread Tools

dice generator problems

 
 
Bill Cunningham
Guest
Posts: n/a
 
      12-09-2009
I had one error in this code until I tried to use error checking with
strtol. Now I've really goofed it. This is the compilation I used.

gcc -g di.c -o a -ansi -pedantic -Wall

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
if (argc > 4 || argc == 0) {
fputs("Dice generator usage error\n", stderr);
exit(EXIT_FAILURE);
}
int x, y;
if (argv[1][0] == '-' && argv[1][1] == 'a') {
if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
fputs("strtol error\n", stderr);
return 1;
}
if ((y = strtol(argv[2], NULL, 10)) == LONG_MIN || LONG_MAX) {
fputs("strtol error\n", stderr);
return 1;
}
printf("%i\n", x + y);
exit(1);
}
srand(time(NULL));
printf("%i\n", rand(void) %argv[1]);
return 0;
}

di.c: In function `main':
di.c:11: warning: ISO C90 forbids mixed declarations and code
di.c:13: error: `LONG_MIN' undeclared (first use in this function)
di.c:13: error: (Each undeclared identifier is reported only once
di.c:13: error: for each function it appears in.)
di.c:13: error: `LONG_MAX' undeclared (first use in this function)
di.c:25: error: syntax error before "void"

Are those return values for strtol right? That must be my biggest
problem. I checked the man page for strtol.

Bill

--

Casts are seldom if ever needed in C.

--Richard Heathfield


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      12-09-2009

"Bill Cunningham" <(E-Mail Removed)> wrote in message
news:4b202935$0$5348$(E-Mail Removed). ..
> I had one error in this code until I tried to use error checking with
> strtol. Now I've really goofed it. This is the compilation I used.
>
> gcc -g di.c -o a -ansi -pedantic -Wall
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
> if (argc > 4 || argc == 0) {
> fputs("Dice generator usage error\n", stderr);
> exit(EXIT_FAILURE);
> }
> int x, y;
> if (argv[1][0] == '-' && argv[1][1] == 'a') {
> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
> fputs("strtol error\n", stderr);
> return 1;
> }
> if ((y = strtol(argv[2], NULL, 10)) == LONG_MIN || LONG_MAX) {
> fputs("strtol error\n", stderr);
> return 1;
> }
> printf("%i\n", x + y);
> exit(1);
> }
> srand(time(NULL));
> printf("%i\n", rand(void) %argv[1]);
> return 0;
> }
>
> di.c: In function `main':
> di.c:11: warning: ISO C90 forbids mixed declarations and code
> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
> di.c:13: error: (Each undeclared identifier is reported only once
> di.c:13: error: for each function it appears in.)
> di.c:13: error: `LONG_MAX' undeclared (first use in this function)
> di.c:25: error: syntax error before "void"
>
> Are those return values for strtol right? That must be my biggest
> problem. I checked the man page for strtol.
>

Wait a minute I didn't balance those parenthesis. Well I think this code
has alot more wrong with it than that.

Bill
--

Casts are seldom if ever needed in C.

--Richard Heathfield



 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      12-09-2009
On 2009-12-09, Bill Cunningham <(E-Mail Removed)> wrote:
> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {


Uh.

x == 1 || 2

is not a test for whether x is equal to either 1 or 2.

> di.c:13: error: `LONG_MIN' undeclared (first use in this function)


Maybe LONG_MIN is declared in a specific header?

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-09-2009

"Seebs" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2009-12-09, Bill Cunningham <(E-Mail Removed)> wrote:
>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

>
> Uh.
>
> x == 1 || 2
>
> is not a test for whether x is equal to either 1 or 2.


I don't quite understand here what you are trying to say.

>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)

>
> Maybe LONG_MIN is declared in a specific header?


stdlib.h

Bill

--

Casts are seldom if ever needed in C.

--Richard Heathfield


 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      12-09-2009
On 2009-12-09, Bill Cunningham <(E-Mail Removed)> wrote:
>
> "Seebs" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 2009-12-09, Bill Cunningham <(E-Mail Removed)> wrote:
>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {


>> Uh.


>> x == 1 || 2


>> is not a test for whether x is equal to either 1 or 2.


> I don't quite understand here what you are trying to say.


I'm saying something really, really, simple. In English.

You have written:
if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

Let's simplify this a bit:
x = strtol(argv[1], NULL, 10);
if (x == LONG_MIN || LONG_MAX) { ...

Now, there are two possibilities.

One is that you think this will tell you whether x is either of LONG_MIN
or LONG_MAX.

The other is that you want to test whether x is 1.

The thing is: You have written something that might be read out loud
as "if x is equal to long_min or long_max". And that suggests, given
your general history, that you have somehow decided that this is a
good way to test whether x has either of these two values.

However, in C, the expression "LONG_MIN || LONG_MAX" turns out to have
the value 1, because at least one of them has a non-zero value. So what
you've actually done is "if x is equal to the boolean value of the
expression long_min or long_max, which is one". So it's equivalent to
"if (x == 1)".

>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)


>> Maybe LONG_MIN is declared in a specific header?


> stdlib.h


#include <stdlib.h>
int main(void) {
int x = LONG_MIN;
}

gcc says:
t.c: In function "main":
t.c:3: error: "LONG_MIN" undeclared (first use in this function)
t.c:3: error: (Each undeclared identifier is reported only once
t.c:3: error: for each function it appears in.)

So I guess no, it must be some other header. Man, sure is a shame it's
impossible to open the documentation and look, or we'd have this one nailed.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-09-2009
"Bill Cunningham" <(E-Mail Removed)> writes:
> "Seebs" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 2009-12-09, Bill Cunningham <(E-Mail Removed)> wrote:
>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

>>
>> Uh.
>>
>> x == 1 || 2
>>
>> is not a test for whether x is equal to either 1 or 2.

>
> I don't quite understand here what you are trying to say.


Read it again until you do.

>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)

>>
>> Maybe LONG_MIN is declared in a specific header?

>
> stdlib.h


Don't guess.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-09-2009

"Seebs" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2009-12-09, Bill Cunningham <(E-Mail Removed)> wrote:
>>
>> "Seebs" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> On 2009-12-09, Bill Cunningham <(E-Mail Removed)> wrote:
>>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

>
>>> Uh.

>
>>> x == 1 || 2

>
>>> is not a test for whether x is equal to either 1 or 2.

>
>> I don't quite understand here what you are trying to say.

>
> I'm saying something really, really, simple. In English.
>
> You have written:
> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>
> Let's simplify this a bit:
> x = strtol(argv[1], NULL, 10);
> if (x == LONG_MIN || LONG_MAX) { ...
>
> Now, there are two possibilities.
>
> One is that you think this will tell you whether x is either of LONG_MIN
> or LONG_MAX.
>
> The other is that you want to test whether x is 1.
>
> The thing is: You have written something that might be read out loud
> as "if x is equal to long_min or long_max". And that suggests, given
> your general history, that you have somehow decided that this is a
> good way to test whether x has either of these two values.
>
> However, in C, the expression "LONG_MIN || LONG_MAX" turns out to have
> the value 1, because at least one of them has a non-zero value. So what
> you've actually done is "if x is equal to the boolean value of the
> expression long_min or long_max, which is one". So it's equivalent to
> "if (x == 1)".


OK. It's been awhile since I tried error checking What I want if if
(x=y) I need the comparison equals and not the assignment operator.

>>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)

>
>>> Maybe LONG_MIN is declared in a specific header?

>
>> stdlib.h

>
> #include <stdlib.h>
> int main(void) {
> int x = LONG_MIN;
> }
>
> gcc says:
> t.c: In function "main":
> t.c:3: error: "LONG_MIN" undeclared (first use in this function)
> t.c:3: error: (Each undeclared identifier is reported only once
> t.c:3: error: for each function it appears in.)
>
> So I guess no, it must be some other header. Man, sure is a shame it's
> impossible to open the documentation and look, or we'd have this one
> nailed.


I got that from man 3 strtol. It said that strtol was defined in
stdlib.h. It didn't say that LONG_MAX and LONG_MIN were declared else where
but as you have pointed out the compiler says they are somewhere else.

Bill
--

Casts are seldom if ever needed in C.

--Richard Heathfield



 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-09-2009

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Bill Cunningham" <(E-Mail Removed)> writes:
>> "Seebs" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> On 2009-12-09, Bill Cunningham <(E-Mail Removed)> wrote:
>>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>>>
>>> Uh.
>>>
>>> x == 1 || 2
>>>
>>> is not a test for whether x is equal to either 1 or 2.

>>
>> I don't quite understand here what you are trying to say.

>
> Read it again until you do.


I should have used = instead of ==

>>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
>>>
>>> Maybe LONG_MIN is declared in a specific header?

>>
>> stdlib.h

>
> Don't guess.


man 3 strtol said strtol was declared in stdlib.h. But the compiler says
otherwise.

Bill
--

Casts are seldom if ever needed in C.

--Richard Heathfield



 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-10-2009
"Bill Cunningham" <(E-Mail Removed)> writes:

> I had one error in this code until I tried to use error checking with
> strtol. Now I've really goofed it. This is the compilation I used.
>
> gcc -g di.c -o a -ansi -pedantic -Wall
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
> if (argc > 4 || argc == 0) {
> fputs("Dice generator usage error\n", stderr);
> exit(EXIT_FAILURE);
> }
> int x, y;


Why int when you set the using strtol? Surely you mean long here, no?

> if (argv[1][0] == '-' && argv[1][1] == 'a') {
> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {


This is wrong but it not (currently) a problem because errors prevent
compilation. You want x == LONG MAX at the end, not just LONG_MAX.
LONG_MAX on its own will be taken as true so the whole || will always
be true.

> fputs("strtol error\n", stderr);
> return 1;
> }
> if ((y = strtol(argv[2], NULL, 10)) == LONG_MIN || LONG_MAX) {
> fputs("strtol error\n", stderr);
> return 1;
> }
> printf("%i\n", x + y);
> exit(1);
> }
> srand(time(NULL));
> printf("%i\n", rand(void) %argv[1]);


One error message is telling you that rand(void) is wrong. That is
not ho you call a function that takes no arguments. Once you fix
that, there will be a complaint about % argv[1] as well. argv[1] is
of type char * but % requires integer operands.

> return 0;
> }
>
> di.c: In function `main':
> di.c:11: warning: ISO C90 forbids mixed declarations and code
> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
> di.c:13: error: (Each undeclared identifier is reported only once
> di.c:13: error: for each function it appears in.)
> di.c:13: error: `LONG_MAX' undeclared (first use in this function)
> di.c:25: error: syntax error before "void"
>
> Are those return values for strtol right? That must be my biggest
> problem. I checked the man page for strtol.


There are problems with the return values you use (not least trying to
put a long int into and int) but the basic problem is exactly what the
error messages say: you are using names that are not declared.
LONG_MIN and LONG_MAX are defined in limits.h.

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-10-2009
Seebs <(E-Mail Removed)> writes:
[...]
> Let's simplify this a bit:
> x = strtol(argv[1], NULL, 10);
> if (x == LONG_MIN || LONG_MAX) { ...

[...]
> However, in C, the expression "LONG_MIN || LONG_MAX" turns out to have
> the value 1, because at least one of them has a non-zero value. So what
> you've actually done is "if x is equal to the boolean value of the
> expression long_min or long_max, which is one". So it's equivalent to
> "if (x == 1)".

[...]

Not quite. Remember that "==" binds more tightly than "||".
(x == (LONG_MIN || LONG_MAX))
has the meaning you describe, but
(x == LONG_MIN || LONG_MAX)
is equivalent to
((x == LONG_MIN) || LONG_MAX)
which evaluates to 1, regardless of the value of x (assuming, of
course, that LONG_MIN and LONG_MAX are declared by #including the
proper header).

Of course this doesn't change the basic point, which is that
if (x == LONG_MIN || LONG_MAX)
is the wrong way to test whether x is equal either to LONG_MIN or to
LONG_MAX.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dice Frequency Simulation cb17890@yahoo.com Java 1 10-16-2007 04:17 AM
Dice Frequency Simulation cb17890@yahoo.com Java 13 10-14-2007 09:48 AM
Dice gen and analyser script for RPGs: comments sought Richard Buckle Python 4 09-13-2006 07:18 PM
Problems with email.Generator.Generator Chris Withers Python 20 09-12-2006 11:13 AM
Dice probability problem Tomi Lindberg Python 11 04-06-2006 08:17 AM



Advertisments