Question: Floating Point Exception(core dumped) in c. I created a program that takes in an arithmetic expression and performs it from left to right(ignoring order of
Floating Point Exception(core dumped) in c. I created a program that takes in an arithmetic expression and performs it from left to right(ignoring order of operations). Everything in the program works except division, every time I use input with division I get the floating point exception. I cannot see what I am doing wrong here, please help. Here is my code:
#include
int main(void){ /*create an array for the input problem, prompt user to enter an arithmetic expression*/ char problem[20]; //prompt the user printf("enter an arithmetic expression to evaluate."); fflush(stdout); //use fgets to get the string stored fgets(problem,20,stdin); /*create a seperate array for the operational characters(+, -, /, and *) for the integers contained in the problem. Then loop through the problem String and seperate them into the given arrays*/ char operations[20]; int numbers[20]; int i = 0; int numCount = 0; int charCount = 0; for(i = 0; problem[i] != '\0'; i++){ if(problem[i] >= 0x30 && problem[i] <= 0x39){ numbers[numCount] = problem[i] - '0'; numCount++; } if(problem[i]=='+'||problem[i]=='-'||problem[i]=='*'||problem[i]=='/'){ operations[charCount] = problem[i]; charCount++; } } /*loops through both arrays and performs the specified operation on the integers*/ int total; int tracker = 0; for(i = 0;i < numCount;i++){ if(operations[tracker] == '+'){ total = numbers[i] + numbers[i + 1]; numbers[i + 1] = total; tracker++; }else if(operations[tracker] == '-'){ total = numbers[i] - numbers[i + 1]; numbers[i + 1] = total; tracker++; }else if(operations[tracker] == '/'){ total = numbers[i] / numbers[i + 1]; numbers[i + 1] = total; tracker++; }else if(operations[tracker] == '*'){ total = numbers[i] * numbers[i + 1]; numbers[i + 1] = total; tracker++; } } //prints out the solution for the user printf("The answer is: %d ", numbers[i - 1]); fflush(stdout);
return 0 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
