第一次scanf新手c编程后崩溃

时间:2015-10-18 02:50:58

标签: c scanf

我的代码( gist link

//pre-processor directives
#include <stdio.h>
#include <math.h>

//Main function
int main() {
    int month, day, year, lowest_temp, highest_temp;
    float temp;
    char fname[30];

    FILE * ifp;

    printf("Tell me about your dragon's preferred temperature for flying: What is the coldest temperature they can fly in?\n");
    scanf("%d", lowest_temp);
    printf("What is the hottest temperature they can fly in?\n");
    scanf("%d", highest_temp);
    printf("Please enter the name of the weather data file for Dragon Island.\n");
    scanf("%s", fname);

    ifp = fopen(fname, "r");
    fscanf(ifp, "%d %d %d %f, &month &day &year &temp");

    printf("%d %d %d %f, &month &day &year &temp");

    return 0;

}

首次扫描后崩溃没有错误

如果你能告诉我错误是什么,我会很感激。另外,如果你可以帮助我解决我的问题的下一步,那将是非常棒的。

我的任务 https://gist.github.com/anonymous/e9292f14b2f8f71501ea/88e9e980632871f1f1dedf7589bb518f1bba43e8

1 个答案:

答案 0 :(得分:3)

 scanf("%d", lowest_temp); 
 ...
 scanf("%d", highest_temp); 

%d要求int参数的地址只传递int变量。传递他们的地址 -

scanf("%d",&lowest_temp); 
...
scanf("%d",&highest_temp); 

这两个陈述 -

fscanf(ifp, "%d %d %d %f, &month &day &year &temp");
printf("%d %d %d %f, &month &day &year &temp");   

应该是 -

fscanf(ifp, "%d %d %d %f", &month &day &year &temp);
printf("%d %d %d %f", month ,day,year ,temp);