Error in executing the C program

Hello Friends,

I have written a code for the unisex bathroom which makes a policy that when a woman is in the bathroom only other women may enter, but not men, and vice versa. This program consists of four functions which a user defines but these functions are not properly working while executing the program.

When i tried to compile the code, it is failing due to the below error :

sh-4.2# gcc -o main *.c                                                     
/tmp/cczKtK6E.o: In function `main':                                        
main.c:(.text+0x54e): undefined reference to `man_leaves'                   
main.c:(.text+0x560): undefined reference to `woman_leaves'                 
collect2: error: ld returned 1 exit status                                  
sh-4.2#  

Could you please help me out with this :

#include<stdio.h>
#include<stdlib.h>

// Defining all the variables that are used in below user defined functions
int max_queue=0;
int max_bathroom=0;
int men_bathroom=0;
int women_bathroom=0;
int men_queue=0;
int women_queue=0;
int cycle=1;
int turn=0;



void man_wants_to_enter()
{
// Checking for man whether he can enter the queue
// If there are women in bathroom or women in queue and the maximum strength of queue is less than the max limit then
// a man can enter the bathroom

if( max_queue < 5 && max_bathroom < 5 && women_bathroom > 0 || women_queue > 0 )
{
//if the above condition is true then a man enters the queue 
if ( max_bathroom == 0 )
{
printf(" Status of Bathroom : EMPTY \n ");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
}


men_queue++;
max_queue++;
printf(" please Wait in the Queue for your turn \n");
printf(" Man Enters the Queue \n");
printf(" \n Cycle - %d\n",cycle); 
printf(" Status of Queue : Strength of Men - %d \n",men_queue);
cycle++;
}
// if there aren't any women in the bathroom,there is enough space in bathroom then 
// a man can enter the washroom
else if(max_bathroom < 5)
{
if ( max_bathroom == 0 )
{
printf(" Status of Bathroom : EMPTY \n ");
}
men_bathroom++;
max_bathroom++;
// When a man enters bathroom, then men strength inside and total strength too increases
if(men_queue > 0){
// If there are men in the queue then their strength decreases as soon as a man enters bathroom
men_queue--;
max_queue--;
printf(" Man Exits the Queue \n " );
printf(" \n Cycle - %d\n",cycle);
 cycle++;
printf(" Status of Queue : Strength of men- %d \n",men_queue);
}
printf(" First Man in the Queue , Please Enter the bathroom \n");
printf(" Man Enters bathroom \n ");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of Bathroom : Occupied by %d number of Men \n",men_bathroom);
}
else
{ printf(" 5 People maximum Allowed , Please wait for your turn \n");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
// This message shows up when someone asks for bathroom and when it is full
}

void man_leaves()
{ 
// when there are men in the bathroom and when they leave
if ( men_bathroom > 0 )
{
// Strength of bathroom and men decreases when someone leaves
max_bathroom--;
men_bathroom--;
printf(" Man leaves Bathroom \n ");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of Bathroom : Strength of Men - %d\n",men_bathroom);
}
// When there are no men in the bathroom then women gets to enter the bathroom if they are in the queue first
if ((men_bathroom == 0)&& (women_queue > 0) && (max_bathroom < 5))
{
// Since the bathroom strenth is zero and women are in queue , they get to enter until there are no women in the queue
while ((women_queue > 0))
{
//while loop is to make sure every woman gets to enter the bathroom when there are no men
max_queue--;
women_queue--;
printf(" Women Leaves the queue \n");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of Queue : Strength of women- %d\n",women_queue);
women_bathroom++;
max_bathroom++;
printf(" Woman Enters Bathroom \n");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of Bathroom : Strength of Women - %d\n",women_bathroom);
}
}
else if((men_queue > 0) && ( max_bathroom < 5))
{
// if there no women in the queue and there are men in the bathroom then men gets to enter the bathroom and leave the queue
men_bathroom++;
max_bathroom++;
printf(" Man Enters the Bathroom \n");
printf(" \n Cycle - %d\n",cycle);
 cycle++;
printf(" Status of Bathroom : Strength of Men - %d\n",men_bathroom);
max_queue--;
men_queue--;
printf(" Men Leaves the Queue \n");
printf(" \n Cycle - %d\n",cycle);
 cycle++;
printf(" Status of Queue : Strength of Men - %d \n",men_queue);

}}}

void woman_wants_to_enter()
{
// Similar to the function man_wants_to_enter() , Here Woman requests for bathroom


if( max_queue < 5 && max_bathroom < 5 && men_bathroom > 0 || men_queue > 0 )
{
if ( max_bathroom == 0 )
{
printf(" Status of Bathroom : EMPTY \n ");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
}


women_queue++;
max_queue++;
printf(" please Wait in the Queue for your turn \n");
printf(" Woman Enters the Queue \n");
printf(" \n Cycle - %d\n",cycle); 
printf(" Status of the Queue : Strength of Women - %d\n",women_queue);
cycle++;
}

else if(max_bathroom < 5)
{
if ( max_bathroom == 0 )
{
printf(" Status of Bathroom : EMPTY \n ");
}
women_bathroom++;
max_bathroom++;
printf(" Women Enters the Bathroom \n");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of the Bathroom : Strength of Women - %d\n",women_bathroom);
if(women_queue > 0){
// If there are men in the queue then their strength decreases as soon as a man enters bathroom
women_queue--;
max_queue--;
printf(" Woman Exits the Queue \n " );
printf(" \n Cycle - %d\n",cycle);
 cycle++;
printf(" Status of Queue : Strength of women- %d \n",women_queue);
}
printf(" First Woman in the Queue , Please Enter the bathroom \n");
printf(" Woman Enters bathroom \n ");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of Bathroom : Occupied by %d number of Women \n",women_bathroom);
}
else
{ printf(" 5 People maximum Allowed , Please wait for your turn \n");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
// This message shows up when someone asks for bathroom and when it is full
}

void woman_leaves()
{ 
// Similar to the function man_leaves(), Here women leaves the bathroom
if ( women_bathroom > 0 )
{
max_bathroom--;
women_bathroom--;
printf(" Woman leaves Bathroom \n ");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of Bathroom : Strength of Women - %d\n",men_bathroom);
}
if ((women_bathroom == 0)&& (men_queue > 0) && (max_bathroom < 5))
{
while ((men_queue > 0))
{
max_queue--;
men_queue--;
printf(" Man Leaves the queue \n");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of Queue : Strength of men- %d\n",women_queue);
men_bathroom++;
max_bathroom++;
printf(" Man Enters Bathroom \n");
printf(" \n Cycle - %d\n",cycle); 
cycle++;
printf(" Status of Bathroom : Strength of Men - %d\n",women_bathroom);
}
}
else if((women_queue > 0) && ( max_bathroom < 5))
{
// if there no women in the queue and there are men in the bathroom then men gets to enter the bathroom and leave the queue
women_bathroom++;
max_bathroom++;
printf(" Woman Enters the Bathroom \n");
printf(" \n Cycle - %d\n",cycle);
 cycle++;
printf(" Status of Bathroom : Strength of Women - %d\n",men_bathroom);
max_queue--;
women_queue--;
printf(" Women Leaves the Queue \n");
printf(" \n Cycle - %d\n",cycle);
 cycle++;
printf(" Status of Queue : Strength of Women - %d \n",men_queue);

}}}

	main()
	{
	printf(" \n Hello , Welcome to the City Unisex Bathroom \n ");
	srand(time(NULL));
	int random = rand()% 2 + 1; 
	int gender = rand()%2 +1;
		while ( turn < 20 )
		{
			if ( random == 1) {
			if ( gender == 1) {
			printf(" \n Bathroom Requested by a Man \n");
			man_wants_to_enter();
			break;
			}
			else if ( gender == 2) {
			printf(" \n Bathroom Requested by a Woman \n");
			woman_wants_to_enter();
			break;
			}
			}
			else if ( random == 2) {
			if ( gender == 1) {
			man_leaves();
			break;
			}
			else if ( gender == 2) {
			woman_leaves();
			break;
			}
			}
			break;
		}
	}
	

https://www.google.com/search?q=indent\+code\+for\+readability

1 Like
void man_leaves()
{ 
// when there are men in the bathroom and when they leave
if ( men_bathroom > 0 )
{
// Strength of bathroom and men decreases when someone leaves
max_bathroom--;

This doesn't look right?

As achenle suggested, format your code so the structure is obvious from the indentation! (If you can't make your code readable, why should we waste time reformatting your code so we have a chance of figuring what you're doing wrong.)

Once you do that, you'll probably find that your man_leaves() and woman_leaves() functions are being defined inside code defining other functions instead of defining all of your functions independently.

All of the above. These things show up a lot easier when your code is structured and indented correctly. Consider Vi as an editor.

For starters

add some #includes...

#include <stdlib.h>  // rand(), srand()
#include <time.h>    // time()

Add a return 0; at the end of your methods.
..at or about these lines...approximate
lines: 76, 129, 189, 238, 273

}
return 0;
}

Change return type from void to int in all your functions.

int man_leaves()
{
...
...
int main()
{
...
etc.

Closing brace errors...
line: 124, 230
..too many closing braces.
replace }}} with

}
return 0;
}

...then add closeing brace..
lines: 73, 183

return 0;
}
void man_leaves()
...
return 0;
}
void woman_leaves()

Add parens when mixing && and ||...
line 22, 131

if(( max_queue < 5 && max_bathroom < 5 && women_bathroom > 0 ) || ( women_queue > 0 ))
{