Error in executing the C program


 
Thread Tools Search this Thread
Top Forums Programming Error in executing the C program
# 1  
Old 12-15-2014
Wrench 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 :

Code:
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#

Moderator's Comments:
Mod Comment code tags please

Could you please help me out with this :

Code:
#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;
		}
	}


Last edited by jim mcnamara; 12-15-2014 at 04:34 PM..
# 2  
Old 12-15-2014
This User Gave Thanks to achenle For This Post:
# 3  
Old 12-15-2014
Code:
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?
# 4  
Old 12-15-2014
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.
# 5  
Old 12-15-2014
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 ))
{
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

executing a different program

What system calls or commands do I need to use in order to execute a different program from an already running process? (1 Reply)
Discussion started by: justOne21
1 Replies

2. Shell Programming and Scripting

Executing WIN32OLE program

Hello, Please help me out to execute this perl program: #!/usr/bin/perl -w use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; # die on errors... # get already active Excel application or open new my... (6 Replies)
Discussion started by: suvenduperl
6 Replies

3. Linux

Error while executing Directfb program on ubuntu

Hi , I am getting error while executing Directfb program on ubuntu ,, my program is : #include <stdio.h> #include <unistd.h> #include <directfb.h> static IDirectFB *dfb = NULL; static IDirectFBSurface *primary = NULL; static int screen_width = 0; static int screen_height... (0 Replies)
Discussion started by: Ravikishore
0 Replies

4. Shell Programming and Scripting

Executing a Java Program

I am entirely new to shell scripting and would like to create a script to execute a java program called Main. I've already compiled it and placed the .java and .class files at /root/javaTest. Next I made a shell script that simply contained: java /root/javaTest/Main . I made the script... (2 Replies)
Discussion started by: hypnotic_meat
2 Replies

5. Solaris

How to know the size of the program currently executing in memory

hey everybody, i am currently working on solaris 10 os on a m5000 server. my problem is when i want the exact size of a program in execution, i am unable to do it. earlier i thought the RSS field of prstat but because of its large size it cant be the size. pmap -x shows some output but it includes... (2 Replies)
Discussion started by: aryansheikh
2 Replies

6. Programming

Trubble in executing the cpp program...

I wrote a code like this....... #include <iostream> #include <stdio.h> #include <mysql.h> #include <string.h> #include <stdlib.h> using namespace std; #include "Connection.h" int main() { char *Host = (char *)"localhost"; char *Database =(char *)"sachin"; char... (3 Replies)
Discussion started by: ps_sach
3 Replies

7. Programming

after executing execvp()... program hangs up

Hi , I m actually trying to implement pipes program,but after executing the execvp(),my program is getting hanged up :mad: Actaully i m getting the desired output expected from execvp()...but once results are displayed on the output screen ,program is getting hanged up values of... (3 Replies)
Discussion started by: Crab
3 Replies

8. Programming

Executing an .ec program in different informix versions

Hi all, I tried writing an .ec program connecting to informix database from solaris platform Sun 5.7 informix version that i had to used when i compiled the program was 9.21.UC3 the binary when i ported solaris box with informix version 9.40.UC5 i am unable to run that i am encountering... (0 Replies)
Discussion started by: matrixmadhan
0 Replies

9. UNIX for Dummies Questions & Answers

executing the su command from a java program.

Say in unix (AIX) m/c, I am logged in with s1 user and want to start process p1 with user credentials of s2. I can do manually in this way: #su - s2 #enter password for s2> somePassword $ p1 But all this I have to do through a java program. How to pass the password through program. One... (1 Reply)
Discussion started by: shailendrat
1 Replies

10. Programming

executing a program within a program

Read the title: how do i do it? (4 Replies)
Discussion started by: Gekko
4 Replies
Login or Register to Ask a Question

Featured Tech Videos