非法字符错误来自我的else语句中的打印函数。当我尝试运行该文件时,我收到分段错误: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;
}
将"is't"替换为"isn't"。您使用的撇号字符很奇怪。