C上的浮点异常,无法解决

时间:2014-11-26 11:48:38

标签: c compiler-errors floating-point strcmp

我编写了一个非常简单的代码,当我编译时(我告诉它当我输入END无论如何都应该破坏该程序。)但是有消息显示“浮点异常:8”,我从未见过这个以前的消息,我一直在搜索谷歌告诉我,我有一些浮动值,让程序做一些不应该做的事。

/* 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);
}

2 个答案:

答案 0 :(得分:3)

这可能是因为taxiTotal/taxiCount执行除零 - 这会导致错误(表现为浮点异常,即使您只是使用整数)。

这意味着taxiCount为0,所以你可能宁愿做

if (taxiCount > 0) {
    printf("THERE ARE %d TAXIS TODAY, AVERAGE RANGE ARE %d KM",taxiCount,taxiTotal/taxiCount);
} else {
    puts("THERE ARE NO TAXIS TODAY");
}

答案 1 :(得分:1)

据我所知,当第一个输入为END时,程序会给出异常。基本上,您根本没有输入任何值。

几乎在最后一行:

printf("THERE ARE %d TAXIS TODAY, AVERAGE RANGE ARE %d KM",taxiCount,taxiTotal/taxiCount);

您正在将taxiTotal值除以taxiCount。如果未输入任何值,则除以0,这是不允许的。添加IF语句以在除以之前检查taxicount是否为零。