B
Bill Cunningham
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
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