代码块中的代码崩溃

时间:2016-02-07 05:37:50

标签: c crash codeblocks

所以我是用C编程的新手,但是我有一个相对容易的方程式,而且它似乎不适合它为什么会一直崩溃。

是语法还是有随机方程式崩溃?

#include <stdio.h>
#define Newyork 1077
#define Paris 4487
#define London 4336
#define Rome 5113
#define Frankfurt 4732
#define Sanfrancisco 2888
#define Tokyo 7252
#define Havana 380
// Google maps search

#define B747 614
#define B777 590
#define A330 567
#define A380 634
#define C 1354
// Google maps search
int main (void)  {

int distance, flight_hours, velocity, speed;


char city;
printf ("Please enter the city to which you would like to fly \n");
printf ("Enter the first character of the name of the city in lower case letter \n");
scanf ("%c", city);


switch (city) {
case 'n': {
    distance = Newyork;
    break;
}
case 'p': {
    distance = Paris;
    break;
}
case 'l': {
    distance = London;
    break;
}
case 'r': {
    distance = Rome;
    break;
}
case 'f': {
    distance = Frankfurt;
    break;
}
case 's': {
    distance = Sanfrancisco;
    break;
}
case 't': {
    distance = Tokyo;
    break;
}
case 'h': {
    distance = Havana;
    break;
}
default :
    printf("You've made an error.. \n");
    return 0;

}





printf("Enter the following values for type of aircraft to be flown: \n\n");
printf("Enter 1 if Boeing 747 \n");
printf("Enter 2 if Boeing 777 \n");
printf("Enter 3 if Airbus 330 \n");
printf("Enter 4 if Airbus 380 \n");
printf("Enter 5 if Concorde \n");

scanf("%f", &velocity);


if (velocity == 1)
    speed = B747;
else if (velocity == 2)
    speed = B777;
else if (velocity == 3)
    speed = A330;
else if (velocity == 4)
    speed = A380;
else if (velocity == 5)
    speed = C;
else {
    printf ("Your entry is invalid \n");
    return 0;

}

flight_hours = distance/speed;
((float) (distance%speed)/(float)speed)*60;
return 0;
}

3 个答案:

答案 0 :(得分:0)

从您的switch语句中删除return 0;

应该看起来像

default :
        printf("You've made an error.. \n");

您的scanf()也应该是

scanf("%c", &city);

答案 1 :(得分:0)

scanf ("%c", city);更改为scanf ("%c", &city);

另外,为了使程序每次都正常工作,请执行以下操作:

scanf (" %c", &city);

另外,通过检查scanf()的返回值来检查错误。如果它返回EOF,则会出错。

另外,请检查this问题,了解scanf()的返回值。

答案 2 :(得分:0)

我不知道你正在使用什么编译器,但希望它有警告你可以打开,因为他们应该告诉你你的问题。对我来说,它找到了其他人提到的,还有一个:

% cc -o char char.c
char.c:26:14: warning: format specifies type 'char *' but the argument has type 'int' [-Wformat]
scanf ("%c", city);
        ~~   ^~~~
char.c:78:17: warning: format specifies type 'float *' but the argument has type 'int *' [-Wformat]
    scanf("%f", &velocity);
           ~~   ^~~~~~~~~
           %d
char.c:98:44: warning: expression result unused [-Wunused-value]
    ((float) (distance%speed)/(float)speed)*60;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
3 warnings generated.