分段故障:11&字符串文字中的非法字符编码

时间:2015-03-06 20:43:41

标签: c segmentation-fault illegal-characters

非法字符错误来自我的else语句中的print函数。当我尝试运行该文件时,我收到了一个分段错误:11。该计划的目标是输入0-80的下限温度和大于下限但小于90的上限温度。以及该范围内所需的天数百分比。

#include <stdio.h>

int main(){
    // Enter information in regards to your city
    int percentage_range;
    double low_bound, upper_bound, temperature;
    printf("Please enter the lower temperature bound in Fahrenheit.\n");
    scanf("%lf", &low_bound);
    printf("Please enter the upper temperature bound in Fahrenheit.\n");
    scanf("%lf", &upper_bound);
    printf("Please enter the percentage of days needed in the range.\n");
    scanf("%d", &percentage_range);

    // Read in the file name
    char filename[20];
    printf("Please enter the name of your weather data file for your city.\n");
    scanf("%s", filename);

    // Open the file
    FILE* ifp = fopen(filename, "r");

    int month, day, year;
    fscanf(ifp, "%d", &month);

    // looping totals
    double sum = 0;
    double sum_range = 0;
    int numdays = 0;
    int num_totaldays = 0;
    double percentage;

    while (month != -1){
            num_totaldays++;

        // Read in the rest of the file line
        fscanf(ifp, "%d,%d,%lf",&day, &year, &temperature);

        while (low_bound <= upper_bound){
            sum = sum + temperature;
            numdays++;
            percentage = sum/numdays;
            printf("Days in between %lf and %lf degrees:%d", low_bound, upper_bound, numdays);
            printf("Total number of days:%d",num_totaldays );
            printf("Percentage of days in range:%lf",percentage);
// if the percentage is in range
        if (percentage >= percentage_range){
            printf("Great, I recommend opening a Pizza Shack at your location!\n");
        }
// if not print this 
            else
                printf("Sorry, your weather isn’t temperate enough for Pizza Shack.");

        }





    }


//close file
fclose(ifp);
return 0;
}

1 个答案:

答案 0 :(得分:0)

替换&#34;不是&#34;与&#34;不是&#34;。你使用的撇号字符很奇怪。