I wrote a very simple code and when I compiled (Which I told it that when I enter END whatever case it should break that program.) but there is message show up "Floating point exception: 8", I never saw this message before and I have been searching Google which told me that I have something going on with float value that allow program to do something that it shouldn't.
/* Program: Taxi Day
*
* Description: Calculate fartest taxi distance.
*
* Creator: Thunpisit Amnuaikiatloet #57070503418
*
* Create on: November. 26, 2014 17.55.00
*
* Lastest modify on: November. 26, 2014 17.55.00
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
char buffer[128];
char END[8] = ("END");
char MAXNAME[128];
int MAXRANGE = 0;
char taxiNumber[32];
int taxiStart = 0;
int taxiEnd = 0;
int taxiTotal = 0;
int taxiCount = 0;
while(1)
{
/* Ask user to enter Taxi Number */
printf("\nENTER TAXI NUMBER: ");
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer,"%s",taxiNumber);
/* If Taxi Number entered is END, stop asking */
if(strcasecmp(taxiNumber,END) == 0)
{
break;
}
/* Check Taxi Number validate */
while(strlen(taxiNumber) > 10)
{
printf("ENTER TAXI NUMBER: ");
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer,"%s",taxiNumber);
}
/* Ask user to enter start km */
printf("ENTER START KM?: ");
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer,"%d",&taxiStart);
/* Check Taxi Start validate */
while(taxiStart < 0)
{
printf("ENTER START KM?: ");
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer,"%d",&taxiStart);
}
/* Ask user to enter end km */
printf("ENTER END KM?: ");
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer,"%d",&taxiEnd);
/* Check Taxi Start validate */
while(taxiStart > taxiEnd)
{
printf("ENTER END KM?: ");
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer,"%d",&taxiEnd);
}
/* Calculate total distance for each taxi */
taxiTotal = taxiEnd - taxiStart;
printf("----------------------------------");
/* Show taxi entered information */
printf("\nTAXI %s HAS TRAVELED %d KM\n",taxiNumber,taxiTotal);
/* Compute the taxi that have largest distance */
if(MAXRANGE < taxiTotal)
{
MAXRANGE = taxiTotal;
strcpy(MAXNAME,taxiNumber);
}
/* Count number of taxi entered */
taxiCount++;
}
/* Print Summary */
printf("\nTAXI %s DROVE THE LONGEST RANGE, WHICH IS %d KM",MAXNAME,MAXRANGE);
printf("THERE ARE %d TAXIS TODAY, AVERAGE RANGE ARE %d KM",taxiCount,taxiTotal/taxiCount);
}
This likely happens because
taxiTotal/taxiCount
performs a division by zero - which results in an error (manifested as a floating point exception, even if you're just using integers).That means
taxiCount
is 0, so you should probably rather do