Thread: unexpected output

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    31

    unexpected output

    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.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You need to pass roomnum as a pointer if you want findfree to change the value of it.

    Code:
    findfree(&roomnum);
    
    int findfree(int *room)
    {
       ...
       *room = i;
       ...
    }
    if roomnum is already a pointer in main then you don't need the ampersand, but you do need to dereference it in your printf


    Example.
    Code:
    #include <stdio.h>
    
    void change(int *x, int val)
    {
        *x = val;
    }
    
    int main(void)
    {
        int i = 0;
        int *p = &i;
    
        change(&i, 3);
        printf("%d\n", i);
    
        change(p, 6);
        printf("%d\n", i);
    
        return 0;
    }
    You don't need to use pointers at all. You could just return room
    You can set room to -1 if no room available, before you return it.
    Code:
    if((roomnum = findfree()) < 0)
    Last edited by spydoor; 05-16-2006 at 07:41 AM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    Thanks. I still get confused with pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM