Simple Logic problem

This is a discussion on Simple Logic problem within the C Programming forums, part of the General Programming Boards category; I'm having trouble with a small segment of code which should find out wether the number entered is less than ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    15

    Simple Logic problem

    I'm having trouble with a small segment of code which should find out wether the number entered is less than or equal to zero. The do while loop asking for employee age wont seem to work. When ever I enter a negative number it accepts it and won't carrying on prompting for a correct age.

    Code:
    /* Add new employee to database */
    static void menu_add_employee(char *line)
    {
      new_employee = malloc(sizeof(struct Employee));
      
      fprintf ( stderr, "Enter employee name:\n");
      read_line(stdin, line, MAX_NAME_LENGTH);
      strcpy (new_employee->name, line);
      
      do
      { 
        fprintf ( stderr, "Enter employee sex:\n");
        read_line(stdin, line, 2);
        new_employee->sex = *line;
      } while((line[0] != 'M')&&(line[0] != 'F'));
      
      do
      {
        fprintf ( stderr, "Enter employee age:\n");
        read_line(stdin, line, 4);
        new_employee->age = *line;
      } while((new_employee->age) <= 0); 
      
      fprintf ( stderr, "Enter employee job:\n");
      read_line(stdin, line, MAX_JOB_LENGTH);
      strcpy (new_employee->job, line);
    
      new_employee->next = NULL;
      if (employee_list_start==NULL) 
         employee_list_start = new_employee; 
      employee_list_end = new_employee;
      
    }
    Here is the structure definition:

    Code:
    /* Employee structure */
    struct Employee
    {
       char name[MAX_NAME_LENGTH+1]; /* name string */
       char sex;                     /* sex identifier, either 'M' or 'F' */
       int  age;                     /* age */
       char job[MAX_JOB_LENGTH+1];   /* job string */
       
       /* pointers to previous and next employee structures in the linked list */
       struct Employee *prev, *next;
    };
    
    static struct Employee *employee_list_start = NULL;
    static struct Employee *employee_list_end = NULL;
    
    struct Employee *new_employee;
    Here is the code for the read_line function :

    Code:
    /* Read line of characters from file pointer "fp", copying the characters
       into the "line" string, up to a maximum of "max_length" characters, plus
       one for the string termination character '\0'. Reading stops upon
       encountering the end-of-line character '\n', for which '\0' is substituted
       in the string. If the end of file character EOF is reached before the end
       of the line, the failure condition (-1) is returned. If the line is longer
       than the maximum length "max_length" of the string, the extra characters
       are read but ignored. Success is returned (0) on successfully reading
       a line */
    static int read_line ( FILE *fp, char *line, int max_length )
    {
       int i;
       char ch;
    
       /* initialize index to string character */
       i = 0;
    
       /* read to end of line, filling in characters in string up to its
          maximum length, and ignoring the rest, if any */
       for(;;)
       {
          /* read next character */
          ch = fgetc(fp);
    
          /* check for end of file error */
          if ( ch == EOF )
    	    return -1;
    
          /* check for end of line */
          if ( ch == '\n' )
          {
    	   line[i] = '\0';
    	   return 0;
          }
    
          /* fill character in string if it is not already full*/
          if ( i < max_length )
    	    line[i++] = ch;
       }
    
       /* the program should never reach here */
       return -1;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because you are setting the age to the value of the first character of a string. If you check, probably your negative number is being interpreted as 45.
    Last edited by MK27; 03-06-2010 at 02:06 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Ahh I see, how can I set the age to the whole number and not just the first character?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    So you're getting a string and you want the integral representation of the "number" in that string? Search around for the "atoi" function if so.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by will of fortune View Post
    Ahh I see, how can I set the age to the whole number and not just the first character?
    That "read_line" function is fine, but you may want to combine it with another function that uses it to return a number or a separate function for use in getting numerical input.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Josephus problem variant logic troubles
    By misterMatt in forum C++ Programming
    Replies: 0
    Last Post: 04-29-2009, 03:38 PM
  2. Simple Bouncing Logic
    By SMurf in forum Game Programming
    Replies: 4
    Last Post: 10-27-2006, 11:37 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21