Hello,

I am new in C programming, I am writing a function to evaluate a prefix arithmetic expression. I know my code works well but it seems the message floating exception (core dumped) is because if my (preval / 0) the program is not going to work. However, I would like to add this condition to avoid the the floating exception (core dumped)

if (function == 0) {puts("Division by zero is invalid"); exit(0);}

This is some of my 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 */
        return preval(f) / preval(f);

        }

        /* numeric result of the prefix */

        return token;

        }




Actually I do need to know the compiler and environment so I can see if we can handle the FPE exception. I double checked the link and it looks spot on for handling a divide by zero exception. However a new programmer may be unaware of error handling. It's a slightly advanced topic so when I bring it up, I like to share a link with working code.

You seem to be using the Comment feature instead of replying. Look at the bottom of this page and see/use "Reply to this Topic"

I have to ask.

  1. Why isn't a good idea to test for a possible divide by zero before performing the math?
  2. Why isn't using the fault handling a good idea?
  3. What answers do you expect? Sometimes a new member expects their code to be corrected and finished. If you keep reading here, it's more about learning or sometimes how to research.

Anyways, just a waste of time here, stack overflow, the best.

commented: Rude -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.