LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2015, 04:16 PM   #1
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Rep: Reputation: 10
function return value confusion


I have a function that returns either -1 or 1. When I use the syntax:
Code:
if (isempty())  // I think this should return false iff the function isempty returns true
   return -1;
else
    return 1;
this code does not work but when I try it w/ the 1 explicit it does:
Code:
if ( (isempty()) == 1)
    return -1;
else
    return 1;
Here's the actual code that's giving me problems:
Code:
#include "hotel.h"

int getoccupier(int roomno)
{
	off_t offset;
	ssize_t nread;

	offset = (roomno -1) * NAMELENGTH;

	/* find  room slot and read occupiers name */
	if (lseek(infile, offset, SEEK_SET)	== -1) {
		perror("perror");
		exit(1);
	}

	if ( (nread = read(infile, namebuf, NAMELENGTH)) <= 0) {
		perror("read");
		exit(1);
	}
	/* create a string by replacing the newline char w/ the null terminator */
	namebuf[nread-1] = '\0';
	if ( (isempty()) == 1)
		return -1;
	return 1;
}
include "hotel.h" 

int
findfree()
{
	off_t offset;
	int j;
	ssize_t nread;

	for(j = 1; j <= NROOMS; j++)
	{
		offset = (j-1) * NAMELENGTH;
		if ( lseek(infile, offset, SEEK_SET) == -1) {
			perror("lseek in findfree");
			exit(1);
		} else {
			if ( (nread = read(infile, namebuf, NAMELENGTH)) <= 0) {
				perror("read in findfree");
				exit(1);
			} else 
				namebuf[nread-1] = '\0';
		}
		if ( (isempty()) == 1) 
			return j;

	}
	return -1;
}



int
isempty()
{
	if ( (strncmp(namebuf, empty, strlen(empty))) == 0) 
		return 1;
	else 
		return -1;
}

int
main(int argc, char **argv)
{
	int j;
	int c;
	int roomno;
	infile = openfile();


	printf("enter number: ");
    scanf("d", &roomno);
    if ( (getoccupier(roomno)) == -1)
		printf("Room is empty\n");
	else
	  	printf("%s occupies room %d\n", namebuf, roomno);

/*	
	while (1) {
		printf("\n\n\nGet Occupier 1:\nFree Room 2:\nAdd Guest 3:\nFind Free Room 4:\n\n\nChoose: ");
		c = getchar();
		switch(c) {
			case '1': printf("Enter room number: ");
					  scanf("%d", &roomno);
					  if ( (getoccupier(roomno)) == -1)
					  	printf("Room is empty\n");
					  else
					  	printf("%s occupies room %d\n", namebuf, roomno);
					  break;
			case '2': printf("Enter room number to clear: ");
					  scanf("%d", &roomno);
					  if  ((freeroom(roomno)) == -1)
					  	printf ("Room is already empty\n");
					  else
					  	printf("%d was emptied\n", roomno);
					  break;
			case '3': if ((roomno = addguest()) == -1) 
						printf("No rooms available\n");
					  else
					  	printf("%s was added to room %d\n", namebuf, roomno);
					  break;
			case '4': if ((roomno = findfree()) == -1)
						printf("No available rooms\n");
					  else
					  	printf("%d is free\n", roomno);
					  break;
		}
	}
*/
	return 0;
}

Last edited by doughyi8u; 04-27-2015 at 04:19 PM.
 
Old 04-27-2015, 04:20 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
In C both 1 and -1 are true. 0 is false. Making up your own convention leads to confusion.
 
Old 04-27-2015, 04:28 PM   #3
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
I didn't know -1 was true. thanks for the help
 
Old 04-28-2015, 12:15 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(In C, zero=false, nonzero=true; for many functions, zero=success, nonzero=error; for some the opposite stands; for others, negative=error, non-negative=success)

Last edited by NevemTeve; 04-28-2015 at 12:18 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusion with main function . tushar_pandey Programming 13 08-30-2012 11:04 PM
return value from a function jamesbon Programming 7 11-17-2010 07:39 AM
Get return value of function mortonmorton Linux - Newbie 4 10-09-2009 09:54 PM
where is return value of a function stored? fssengg Programming 7 06-26-2008 01:43 AM
Bash function return rejeep Programming 15 08-24-2007 04:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration