J
Joe Smith
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUF_MAX 80
int main (void) {
char buf[BUF_MAX];
unsigned long l;
size_t s;
printf("give me a number less than a third billion: \n");
while(fgets(buf, BUF_MAX, stdin) != NULL) {
if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
errno = 0;
l = strtoul(buf, NULL, 10);
if (errno == ERANGE) {
printf("number out of range!\n");
} else {
/* add code here */
}
} else {
while(s--)
putchar(' ');
putchar('^');
printf(" invalid character\n");
}
}
return 0;
}
/* int *dynarray;
dynarray = malloc(l * sizeof(long)); */
/* end source */
I seek to dynamically allocate memory of an array of unsigned longs with
array size determined from an unsigned long from the keyboard. With program
control as it exists now, it would seem that I would get the business done
where I have marked
/* add code here */
Do I need to redesign program control in order not to have a leak the size
of Texas? Joe
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUF_MAX 80
int main (void) {
char buf[BUF_MAX];
unsigned long l;
size_t s;
printf("give me a number less than a third billion: \n");
while(fgets(buf, BUF_MAX, stdin) != NULL) {
if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
errno = 0;
l = strtoul(buf, NULL, 10);
if (errno == ERANGE) {
printf("number out of range!\n");
} else {
/* add code here */
}
} else {
while(s--)
putchar(' ');
putchar('^');
printf(" invalid character\n");
}
}
return 0;
}
/* int *dynarray;
dynarray = malloc(l * sizeof(long)); */
/* end source */
I seek to dynamically allocate memory of an array of unsigned longs with
array size determined from an unsigned long from the keyboard. With program
control as it exists now, it would seem that I would get the business done
where I have marked
/* add code here */
Do I need to redesign program control in order not to have a leak the size
of Texas? Joe