I'm trying to create a function that searches through all the beds in a hotel until it finds one that is available or just ends when all the records are searched. When it finds that bed, it updates that bed record with new information (marking it as reserved or occupied). The problem is that my function keeps on looping just above the fseek. I know this because I placed a simple print statement above it. At the end of it's run the function is suppose to return the room number that's available to the caller.

// ROOM ASSIGNMENT FUNCTION
int room_assigner(int type, int status) // type (1- smoking 2- non-smoking); status (9-reserve 10-vacant 12-occupied)
{
   FILE *roomPtr;
	int r_num = 0; // room number
   int seeker = 1;  // use to traverse through each record

   if((roomPtr = fopen("rooms.txt", "rb+")) == NULL)
   {
   	printf("\nError in opening file");
   }
   else
   {
   	do
      {  // if seeker = 1 then record 1 field room_status should be checked by the if function
      	fseek( roomPtr, ( seeker - 1) * sizeof( ROOM_DATA ), SEEK_SET);
         // read the info. from the file
         fread( &room, sizeof( ROOM_DATA ), 1, roomPtr );

         if( room.room_status == 10 )
         {
         	if( type == 1 )      // smoking
            {
            	room.room_rate = 4000;
            }
            else                         // non-smoking
            {
             	room.room_rate = 3000;
            }
            room.room_num = room.room_num;
            room.room_type = room.room_type;
            room.room_status = status;
            r_num = room.room_num;           // assigns the chosen room
            fwrite( &room, sizeof( ROOM_DATA ), 1, roomPtr );
         }
         else
         {
         	seeker++;
         }
      }while(room.room_status != 10 && !feof(roomPtr)); // loop ends only if a empty room was found or the end of the file is reached
   }

	return r_num;  // return the choosen room, if zero then a room was not found
}

line 32 sets status to something other than 10. That means its value is not 10 when line 40 is executed, so the loop continues.

I would use a different variable, such as bool found = false then set it to true when line 30 is executed.

bool found = false;
do
{
   <snip>
    found = true;
    // line 30 here
    <snip>
} while( found == false && !feof() );

I get the following errors with bool and it still doesn't work when I include <stdbool.h>...

Error: c-project_.c(313,14):Undefined symbol 'bool'
Error: c-project_.c(313,14):Statement missing ;
Error: c-project_.c(329,18):Undefined symbol 'found'
Error: c-project_.c(329,24):Undefined symbol 'true'
Error: c-project_.c(348,28):Undefined symbol 'false'

Error: c-project_.c(7,2):Unable to open include file 'stdbool.h'

I resorted to

int found = 0;
do
{
   <snip>
    found = 1;
    // line 30 here
    <snip>
} while( found == 0 && !feof(roomPtr) );

but the I ave another problem; line 32 doesn't work. When the data is being written back to the file. The status is suppose to change to reserved (or the integer 9) but when I check the file, available (or the integer 10) is still in it's place.

Oops! just define it as an int instead of a bool. bool is defined in c++, and I think the newest version of C too.

#define TRUE 1
#define FALSE 0

int done = FALSE;
do
{
    <snip>
     done = TRUE;
} while( done == FALSE && ...);

>>Error: c-project_.c(7,2):Unable to open include file 'stdbool.h'

Is there such a file?

bool is an afterthought of a boolean type for c added in C99 but it doesn't exit in C89. A macro BOOL exist for C89 but... no need.

#define FALSE 0
#define TRUE (!FALSE)

int found = FALSE;

and inside the loop
found = TRUE;

That's what it saw in Deitel and Deitel C How to Program. Said C99 provides the header file.

I have another problem; line 32 doesn't work. When the data is being written back to the file. The status is suppose to change to reserved (or the integer 9) but when I check the file, I see available (or the integer 10) is still in it's place.

>>Error: c-project_.c(7,2):Unable to open include file 'stdbool.h'

Is there such a file?

Yes, but it is added in the C99 standard.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.