I'm having some trouble with this. It seems basic enough but I keep getting a large integer in the range of 1000-2000 instead of what it looks to me like should be 1-10 or return a negative number.
in main I call it like this:
Code:
case '5': if ((findfree(roomnum)) < 0)
printf("No available rooms\n");
else
printf("%d is available\n", roomnum);
break;
About every time I get a large value that's about 1500 or so
Code:
#include "hotel.h"
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
int fd;
char buf[NLINES];
int
findfree(int room)
{
ssize_t nread;
int i, j;
_Bool vacant;
if (lseek(fd, (off_t)0, SEEK_SET) < 0)
perror("problem with lseek in findfree: line 23");
for (i = 1; i <= MAXROOMS; ++i) {
if ( (nread = read(fd, buf, sizeof(buf))) < 0)
perror("problem with read in findfree function: line 21
vacant = true;
for (j = 0; j < nread; ++j) {
if (isalpha(buf[j])) {
vacant = false;
break;
}
}
if (vacant == true) {
room = i;
return 1;
}
}
return -1;
}
It looks to me like if buf is empty (and it should be just spaces) vacant should remain true and have the value of i passed to it (i being a number between 1 and 10 since MAXROOMS s defined as 10.