Thread: C - programming - Floating exception (core dumped) - division by 0

  1. #1
    Registered User
    Join Date
    Mar 2022
    Posts
    2

    C - programming - Floating exception (core dumped) - division by 0

    Hello,

    I am having this an error " floating exception (core dumped)" it is because I have no control over which call to preval is executed first in the case "/". How can I call preval twice, storing the results in variables, then use those variables in the division. Thank you in advance for your help.

    Code:
    #include <stdio.h>       /* for printf in C programs */
    #include <stdlib.h>      /* for exit() in C programs */
    
    
    
    
    
    
    #define LEN 30
    char  testing[] = "+42+55-6 8" ;
    
    
    /*   gettoken for arithmetic expression.
     *      Only recognize + - * / ( ) and digits.
      *     Any other character ignored, such as space, must be used to separate consecutive numbers
     *      EXIT on end of line or string
     *   Return value:
     *      1. non-negative integer  OR
     *      2. negative of single character token
     *         uses fgetc, ungetc file functions, for stdin, or fmemopen for strings in memory
    */
    #define   isdigit(ch)  ch >= '0' && ch <= '9'
    
    
    int gettoken (FILE * f) {
        int sum = 0;
      while(1) {
        char ch = fgetc(f);         // first unprocessed char
        if (isdigit(ch))
        {   sum = ch&0xF;           // start of number
            ch = fgetc(f);
            while (isdigit(ch))
              {  sum = sum*10 + (ch&0xF);
                 ch = fgetc(f);
              }
            ungetc (ch, f);      // finished number, do not consume non-numeral
            return sum;
        }
        else if (ch=='+'||ch=='-'||ch=='*'||ch=='/')
          return -ch;               // found an operator
        else if (ch==0 || ch=='\n') // no more characters
        {   puts("--no more input--");
            exit(0);
        }
      }     // end while. Ignoring space or other chars.
    }
    int preval(FILE *f);
    FILE *fmemopen(void*,size_t,const char*); // needed for c99
    
    
    int main() {
    //  FILE * membuf = fmemopen(testing, LEN,"r");  // will be used by mipsmark
        puts("Enter a prefix arithmetic expression:");
       printf ("Evaluates to %d\n", preval(stdin));
      return 0;
    }
    
    
    /* Any changes above this line will be discarded by
    # mipsmark. Put your answer between dashed lines. */
    /*-------------- start cut ----------------------- */
    
    
    /*  Student's Name: */
    
    
    int preval(FILE *f)     {
    
    
            /* getting the token */
            int token = gettoken(f);
    
    
    
    
            /* switching the negative of token to branch into any binary operation, if any */
    
    
            switch (-token) {
    
    
            case '+':
            /* addition */
            return preval(f) + preval(f);
    
    
            case '-':
    
    
            /* substraction */
            return preval(f) - preval(f);
    
    
            case '*':
            /* multiplication */
            return preval(f) * preval(f);
    
    
    
    
            case '/':
            /* division */
            
            if (preval(f)==0) {puts("Divisionn by zero is invalid."); exit(0);}
            
            return preval(f) / preval(f);
    
    
            }
    
    
            /* numeric result of the prefix */
    
    
            return token;
    
    
         }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > return preval(f) + preval(f);
    There's no guarantee that the left operand is called first.

    In each case, you need to do this.
    Code:
    float left = preval(f)
    float right = preval(f);
    return left + right;
    And in the case of division.
    Code:
    float left = preval(f)
    float right = preval(f);
    if ( right == 0.0 ) {
      // error
    } else {
      return left / right;
    }
    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
    Registered User
    Join Date
    Mar 2022
    Posts
    2
    Thank you it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped) C programming pthreads
    By Deepak Mudiam in forum C Programming
    Replies: 2
    Last Post: 04-24-2014, 06:38 AM
  2. Floating point exception( core dump) error in my program..
    By nareshlove in forum C Programming
    Replies: 12
    Last Post: 01-21-2013, 02:42 AM
  3. Cause of a seg fault (core dumped)
    By towed in forum C Programming
    Replies: 3
    Last Post: 01-15-2011, 11:41 PM
  4. Arithmetic Exception (Core Dumped) Help!
    By rangerys in forum C Programming
    Replies: 2
    Last Post: 10-07-2010, 07:18 AM
  5. Floating Exception (Core Dumped)
    By DarrenY in forum C Programming
    Replies: 9
    Last Post: 05-14-2007, 10:01 AM

Tags for this Thread