Thread: How do i assign internal error flags?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    How do i assign internal error flags?

    Code:
    int dequeue() {
    	int extracted_int;
    
    	if ( tail_index == -1 ) {
    		return NULL; // this is the part where i am stuck
    	}
    	else {
    		extracted_int = queue[tail_index];
    		tail_index = tail_index - 1;
    	}
    }
    Hi. I'm a noob so even if this sounds ridiculous please don't be mad . I am trying to make a "dequeue" function that returns last element in the queue in int type. The problem is it needs to return some error flag when the queue is empty. The error flag must return internally so that it does not disrupt the entire program. It will only handled by the function that calls it. Assigning a very unlikely integer as an error flag could be done, or i can add another 'if' statement whenever i call the function, but it is too messy and repetitive. What is the clean way to do it? Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    int dequeue ( int *errors ) {
      if ( ... ) {
        *errors = value;
      } else {
        *errors = 0;
      }
    }
    Pass an additional parameter, where you want the error result stored.

    OR
    Code:
    static int lastError = 0;
    int dequeue ( ) {
      if ( ... ) {
        lastError = value;
      } else {
        lastError = 0;
      }
    }
    int getError ( ) {
      return lastError;
    }
    Store an internal error state flag, and provide an additional function (or global variable like errno) for the external callers to use as they wish.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kaniblu View Post
    Code:
    int dequeue() {
    	int extracted_int;
    
    	if ( tail_index == -1 ) {
    		return NULL; // this is the part where i am stuck
    	}
    	else {
    		extracted_int = queue[tail_index];
    		tail_index = tail_index - 1;
    	}
    }
    Hi. I'm a noob so even if this sounds ridiculous please don't be mad . I am trying to make a "dequeue" function that returns last element in the queue in int type. The problem is it needs to return some error flag when the queue is empty. The error flag must return internally so that it does not disrupt the entire program. It will only handled by the function that calls it. Assigning a very unlikely integer as an error flag could be done, or i can add another 'if' statement whenever i call the function, but it is too messy and repetitive. What is the clean way to do it? Thanks in advance
    You almost had it....

    Code:
    int dequeue() {
    	int extracted_int;
    
    	if ( tail_index == -1 ) {
    		return -1; 
    	}
    	else {
    		extracted_int = queue[tail_index];
    		tail_index = tail_index - 1;
                                    return extracted_int;
    	}
    }
    
    
    
    // call from main code
    NextInt = dequeue();
    if (NextInt < 0 )
      { 
         puts("queue is empty") 
         // deal with error here. 
    }
    The thing is to return the value you're after OR return some impossible value that you can watch for if it fails...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 06:53 PM
  2. Internal Compiler Error
    By Elysia in forum C++ Programming
    Replies: 20
    Last Post: 07-05-2008, 03:59 AM
  3. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 01:51 AM
  4. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  5. Linked list question....again
    By dP munky in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2003, 05:59 PM